One of the first challenges I ever encountered with asp.net was the ability to find an existing control within a webpage, For example, how would I find a label, text box, panel etc.
Default.aspx
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Find Label Control</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”lblTest” runat=”server” Text=”Before” /><br /><br />
<asp:Button ID=”btnTest” runat=”server” Text=”Test” onclick=”btnTest_Click” />
</div>
</form>
</body>
</html>
Code Behind
protected void btnTest_Click(object sender, EventArgs e)
{
lblTest.Text = “After”;
}
This works perfect, but what if we wanted to find a label in the masterpage?
The answer is as follows…
Site.Master
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Find Label Control Recursively</title>
<asp:ContentPlaceHolder ID=”head” runat=”server”>
</asp:ContentPlaceHolder>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”test2″ runat=”server” Text=”Morning” />
<asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ runat=”server”>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
/* Will Get Error if using masterpage
Label myLabel = (Label)FindControl(“test2″);
myLabel.Text = “Evening”;
*/
// This will work!
Label myLabel2 = (Label)FindControlRecursive(“test2″);
myLabel2.Text = “Evening”;
}
protected virtual Control FindControlRecursive(string id)
{
return FindControlRecursive(id, this);
}
protected virtual Control FindControlRecursive(string id, Control parent)
{
// If parent is the control we’re looking for, return it
if (string.Compare(parent.ID, id, true) == 0)
return parent;
// Search through children
foreach (Control child in parent.Controls)
{
Control match = FindControlRecursive(id, child);
if (match != null)
return match;
}
// If we reach here then no control with id was found
return null;
}
On running the webpage WebForm1.aspx, the label id ‘test2′ text within the masterpage will show as ‘Evening’.
It’s as simple as that! You can find any control using the FindControlRecursive method above including text boxes, labels, panels etc.
ADVANCED
If you wish to reuse the coding in several pages and don’t want to repeat the code, then enter the code in your custom BasePage class within the App_Data Folder.
WebForm2.aspx.cs
public partial class WebForm2 : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
Label l = (Label)FindControlRecursive(“test2″);
l.Text = “I love asp.net!”;
}
}
BasePage.cs (within App_Data folder)
using System.Web.UI; // don’t forget to add this namespace!
public class BasePage : System.Web.UI.Page
{
protected virtual Control FindControlRecursive(string id)
{
return FindControlRecursive(id, this);
}
protected virtual Control FindControlRecursive(string id, Control parent)
{
// If parent is the control we’re looking for, return it
if (string.Compare(parent.ID, id, true) == 0)
return parent;
// Search through children
foreach (Control child in parent.Controls)
{
Control match = FindControlRecursive(id, child);
if (match != null)
return match;
}
// If we reach here then no control with id was found
return null;
}
}
Thats all there is to it! If you need any further help on this topic, please leave a comment or email me direct thorugh this site.
