Thursday, March 09, 2006

ASPNET 2.0 : Finding control in WebForm that use MasterPage

When you try to lookup a control by passing a control ID to the FindControl method,
it work a little bit differenly depend on whether your WebForm page use MasterPage or not.

If your are not using MasterPage, the following code will simple return a reference to the WebControl with ID "TextBox1".

Control c = FindControl("TextBox1");


However, if your WebForm use MasterPage, the above code won't work. You have to get a reference to the ContentPlaceHolder which contain your control and then call FindControl() on the ContentPlaceHolder object.

The following code show hot to do it. Create the following MasterPage and call it MasterPage.master

<%@ Master Language="C#"
AutoEventWireup="true"
CodeFile="MasterPage.master.cs"
Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1"
runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>


Now, create a WebForm that use MasterPage.master as master page and insert a TextBox into the WebForm. Give the TextBox an ID "txtFindControl".

<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="MasterPageJustFindControl.aspx.cs"
Inherits="MasterPageJustFindControl"
MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:TextBox ID="txtFindControl" runat="server">
</asp:TextBox>
</asp:Content>


Next, go to the code behind file of the web form and insert the following code:

public partial class MasterPageJustFindControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
JustFindControl();
MasterPageFindControl();
}

private void JustFindControl()
{
Control c = FindControl("txtFindControl");

if (c is WebControl)
{
Response.Write("<BR>JustFindControl : WebControl found<BR>");
}
else
{
Response.Write("<BR>JustFindControl : NOT found<BR>");
}

}

private void MasterPageFindControl()
{
ContentPlaceHolder cpholder = (ContentPlaceHolder)Master.
FindControl("ContentPlaceHolder1");

Control c = cpholder.FindControl("txtFindControl");

if (c is WebControl)
{
Response.Write("<BR>MasterPageFindControl : WebControl found<BR>");
}
else
{
Response.Write("<BR>MasterPageFindControl : NOT found<BR>");
}

}

}


When you run the above code, the output should be

JustFindControl : NOT found

MasterPageFindControl : WebControl found


That approach make some sense though. Because a MasterPage can have multiple ContentPlaceHolder(s) which host different WebForm. Each of those WebForm could have controls with the same ID. So, I think this is a way to isolate the controls from being having ID conflict.

Labels:

0 Comments:

Post a Comment

<< Home