|
ASP.NET - Iterating through controls in an update panel.
|
|
| |
Rating: 3 user(s) have rated this article
5.0 out of 5 stars
Posted by: chimaera,
on 10/20/2009,
in category "ASP.NET 2.0"
Views: this article has been read 635 times
I've written a helper method to iterate through a collection of controls on my page and set certain properties based on their type. Without an update panel I could simply pass the collection of controls from my page and it would loop through them all as I expected. After I added an update panel I the textboxes in that panel we're no longer included. Here's the function I created to loop throught the controls:
|
private void IterateControlsCollection(bool IsEnabled, string CssClass, System.Web.UI.ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
{
TextBox t = ctrl as TextBox;
t.Enabled = IsEnabled;
if (CssClass != string.Empty)
t.CssClass = CssClass;
}
if (ctrl is DropDownList)
{
DropDownList ddl = ctrl as DropDownList;
ddl.Enabled = IsEnabled;
}
if (ctrl is CheckBox)
{
CheckBox cb = ctrl as CheckBox;
cb.Enabled = IsEnabled;
}
if (ctrl is Button)
{
Button b = ctrl as Button;
b.Enabled = IsEnabled;
}
}
}
|
In my page load I was calling this function like this:
|
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
IterateControlsCollection(false, "disabled", Page.Form.Controls);
}
}
|
My "fix" was to simply call the function again but this time passing the conrols collection from the update panel:
|
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
IterateControlsCollection(false, "disabled", Page.Form.Controls);
IterateControlsCollection(false, "disabled", UpdatePanel.ContentTemplateContainer.Controls);
}
}
|
It took me a while to find the actual collection of the controls so note that I found it in UpdatePanel.ContentTemplateContainer.Controls.