i used the code on master page and content page pageload individually. but it not working. my code is below.. Please tel me where should i place code
You didn't paste the code correctly. You pasted this.
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);
}
}
}
It should be like this.
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);}
}
}
That piece of code is needed to make it recursive.
An observation: The Page_Load is too fat. you have to refactor it.
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: find control in Asp.net page with Master page
Mar 02, 2012 10:19 AM|LINK
You didn't paste the code correctly. You pasted this.
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); } } }It should be like this.
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); } } }That piece of code is needed to make it recursive.
An observation: The Page_Load is too fat. you have to refactor it.
linkedin | twitter | www.jerryjoseph.net