private void Page_Load(object sender, System.EventArgs e)
{
LoopButtons(Page.Controls);
}
private void LoopButtons(ControlCollection controlCollection)
{
foreach (var control in controlCollection)
{
if (control is Button)
{
// This is sample code
// You get each button here
// Do something else here
Response.Write(((Button)control).Text);
}
if (control.Controls.Count > 0)
{
LoopButtons(control.Controls);
}
}
}
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: find control in Asp.net page with Master page
Mar 01, 2012 11:02 PM|LINK
You have to loop recursively. Try like this.
private void Page_Load(object sender, System.EventArgs e) { LoopButtons(Page.Controls); } private void LoopButtons(ControlCollection controlCollection) { foreach (var control in controlCollection) { if (control is Button) { // This is sample code // You get each button here // Do something else here Response.Write(((Button)control).Text); } if (control.Controls.Count > 0) { LoopButtons(control.Controls); } } }linkedin | twitter | www.jerryjoseph.net