Working with and accessing dynamic controls in C#
Sep9Written by:
2009/09/09 10:55 AM
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.
blog comments powered by 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
|