Introduction
The below code will help you to identify all the child controls from the Parent control and do the action as we wish. Here i have used panel as a parent control and cleared all the text box control value with one shot by using simple method.
Background
Few day back i got a requirement to put plenty no of panels in webpage for different purpose ,and with in that panel controls we had several textboxes , so whenever we switch between the panel ,we should remove the already entered value and show the empty textbox to the client. For that, instead of accessing all the textbox control separetly ,we can access through the for each loop together and perform the action.
Sample code
public void SetTextBoxToNull(Panel pnlobjid)
{
try
{
foreach(Control cntrl in pnlobjid.Controls)
{
if (cntrl is TextBox)
{
TextBox txtbox = cntrl as TextBox;
txtbox.Text = "";
}
}
}
catch (Exception ex)
{
string test = ex.Message;
}
}Hope the above code will be useful.
