The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Working with and accessing dynamic controls in C#

Sep9

Written by:
2009/09/09 10:55 AM RssIcon

A dynamic control in C# is a control that is created at run-time as opposed to design-time. The advantages of C# dynamic controls is that they can be created in response to how the user interacts with the application, or as a result of other influencing factors. Controls can also be created  as a direct result of information stored in a database.

In some development situations you may not know how many controls you need ahead of time. In these cases it is easier to dynamically create web controls in a container control (such as a Panel) on your form while your application is running.

Almost all C# controls can be created dynamically. But there are certain issues associated with dynamic controls. There is nothing automatic from design-time, you have to set all the properties programmatically. Properties like Size and Location have to be set manually by the programmer.

Another issue is manually attaching Events to dynamic controls. The C# code for the events must be pre-written and must be written in a way that each dynamic control will behave appropriately.

Yet another issue is actually getting to the control ID at runtime. In one of my web apps, this was the challenge. I created the controls in a loop. It was easy to create the controls programmatically, but when time came to inspect them and retrieve their properties, that was the challenge.

You see, when creating dynamic controls within a loop, the control id is automatically created, leaving you with an unknown ID, and a difficult challenge to get to it. You might get something like: “ctl00_ContentPlaceHolder1_ASPxRoundPanel1_option1

Following is a great tip, on how to create those controls in a loop and still be able to get to the control programmatically.

The normal approach – Creating dynamic controls

Normally we would create a set of controls and add it to the form or other container like so:

for (int i = 0; i < cnt; i++)
{    
RadioButton rb = new RadioButton();
rb .Text = coverOption[i, 0];
rb .ID = "option" + i.ToString(); //Does not work   
TableCell cCell = new TableCell();
cCell.Controls.Add(rb1[i]);
cCell.Font.Bold = true;
cCell.HorizontalAlign = HorizontalAlign.Right;
chooseRow.Cells.Add(cCell);
}

As you can see the actual control ID needs to be substituted dynamically as well. This is the challenge, how do I then get to that control if I do not know beforehand what the control ID is going to be?

The solution – Dynamic control array.

The solution that I found, is to put the control into a control array. This way each newly added dynamic control is unique, and their ID will be unique as well. But the beauty is that you will be able to access them as per normal.

Check out the following code.

rb1 = new RadioButton[cnt];
for (int i = 0; i < cnt; i++)
{    
rb1[i] = new RadioButton();
rb1[i].Text = coverOption[i, 0];
rb1[i].ForeColor = Color.DarkGreen;
rb1[i].GroupName = "choice"
TableCell cCell = new TableCell();
cCell.Controls.Add(rb1[i]);
cCell.Font.Bold = true;
cCell.HorizontalAlign = HorizontalAlign.Right;
chooseRow.Cells.Add(cCell);
}

Now we can access those controls through the control array. Check out this example.

foreach (var rb in rb1) { Response.Write(rb.Text + " = " + rb.Checked.ToString()+ "; "); }

Neat hey? This has saved me hours of trouble and headaches. No more reflection and recursive finding of deep embedded controls.

Do you have an idea of working with dynamic controls yourself? Let us know your thoughts in the comments below.

New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.

 



Tags:
Categories:
blog comments powered by Disqus

1 comment(s) so far...


Working with and accessing dynamic controls in C#

A dynamic control in C# is a control that is created at run-time as opposed to design-time. The advantages of C# dynamic controls is that they can be created in response to how the user interacts with the application, or as a result of other influencing factors. Controls can also be created as a direct result of information stored in a database. In some development situations you may not know how many controls you need ahead of time. In these cases it is easier to dynamically create web controls in a container control (such as a Panel) on your form while your application is running.
# ZillionsB.com

By TrackBack on  2009/09/09 11:49 AM
 
Blog Updates Via E-mail
 Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
 Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
 Blog Roll
Minimize
Print  
 
Categories
 Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
 Archive
Minimize
Archive
<October 2024>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
 Tags
Minimize
Print  
 
Contact Us Now
 Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print