In Part 1 we looked at how to find a control of a particular type
by enumerating through the Controls collection. In this part we'll look at how to set and
read the property values of the control we're interested in.
Setting a Control's Properties
At this point we know when we're dealing with a particular type of control, but once we find such
a control, how do we tweak its properties? That is, in the if statement above, once I've
found a TextBox I may wish to set its Text property, or its Columns property.
How can we do this?
Intuitively, you may say, well, why not set the TextBox's Columns property like so:
c.Columns = 10;
While this may seem the intuitive approach, unfortunately it is an incorrect one. The reason being is
that c is a variable of type Control - the Control class does not
contain a property called Columns. What we must do is cast the control c
into a TextBox, and then we can set the Columns property like we normally would.
In C# to cast a variable, simple place the type you wish to cast the variable to (in parenthesis) immediately
before the variable, like:
// Cast c to a TextBox
(TextBox) c
To both cast c to a TextBox and set the Columns property in one line of code
we can do:
((TextBox) c).Columns = 10;
(If you are using VB.NET you will need to use the CType function like so:
CType(c, TextBox).Columns = 10)
Hence, to set the Text and Columns properties of each of the dynamically created
TextBoxes we would need to adjust our IterateThroughChildren like so:
int count = 1;
void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
&& c.ID. == null)
{
((TextBox) c).Text = "TextBox " + count.ToString();
((TextBox) c).Columns = 10;
count++;
}
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}
Notice here we are setting the Text property to a string that contains an integer counting
variable (count). This variable is incremented each time we find a new TextBox. Hence,
the n dynamic TextBoxes will have the text messages TextBox 1,
TextBox 2, and so on, up to TextBox n. We also set the Columns
property to a value of 10.
Conclusion
In this article we examined how to find a control in the Controls collection by its type,
and then, once found, how to set some of its properties. This material covered in this article is a
natural extension from the material in last week's article, Dynamic Controls in ASP.NET.
In cases where you are dynamically adding controls, there is likely the need to find those controls
and set (or read) various properties. Hopefully the techniques discussed in this article will help you
in your future endeavors!
A follow-up article has been published! Dynamic Web Controls, Postbacks, and View State
examines using dynamic Web controls that can fully participate in the standard page lifecycle. Specifically, the article
shows when to add controls programmatically so that their view state and values are persisted correctly across postback.