This is the problem: the method returns a NullReferenceException if I call it from a page that has a MasterPage because FindControl is not returning anything. This is a problem ONLY if the page is associated with a MasterPage.
Dim mpContentPlaceHolder As ContentPlaceHolder
Dim mpTextBox As TextBox
mpContentPlaceHolder = _
CType(Master.FindControl("ContentPlaceHolder1"),ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
mpTextBox = CType(mpContentPlaceHolder.FindControl("TextBox1"), TextBox)
If Not mpTextBox Is Nothing Then
mpTextBox.Text = "TextBox found!"
End If
End If
if you want find control in page with masterpages,first you have find control "contentplaceholder"
Eric Fettman
Member
158 Points
48 Posts
FindControl() doesn't work if page has MasterPage
Aug 22, 2006 09:30 PM|LINK
Hi, Folks --
I'm using the following C# static method to populate some DropDownLists from XML files:
public
static void PopulateDD(Page page, List<string> dropDowns){
DataSet ds = new DataSet(); DropDownList select; foreach (string dropDown in dropDowns){
select = (
DropDownList)page.FindControl("dd"+dropDown);ds.ReadXml(page.Server.MapPath(
"~/xml/"+dropDown+".xml"));select.DataSource = ds;
select.DataTextField =
"text";select.DataValueField =
"text";select.DataBind();
ds.Clear();
}
}
I'm calling the method from a code-behind page as follows:
List
<string> dropDowns = new List<string>();dropDowns.Add(
"State");dropDowns.Add(
"Country");dropDowns.Add(
"Industry"); ControlUtils.PopulateDD(this.Page, dropDowns);This is the problem: the method returns a NullReferenceException if I call it from a page that has a MasterPage because FindControl is not returning anything. This is a problem ONLY if the page is associated with a MasterPage.
Any thoughts appreciated.
TIA,
Eric
lostlander
Contributor
3041 Points
607 Posts
Re: FindControl() doesn't work if page has MasterPage
Aug 23, 2006 03:19 AM|LINK
Use Page.Master.FindControl instead.
Eric Fettman
Member
158 Points
48 Posts
Re: FindControl() doesn't work if page has MasterPage
Aug 23, 2006 10:53 AM|LINK
Thanks for your response.
The controls that I'm populating with XML are not in the MasterPage, so I don't think that this is going to work.
Any other ideas?
Eric Fettman
Member
158 Points
48 Posts
Re: FindControl() doesn't work if page has MasterPage
Aug 23, 2006 06:16 PM|LINK
I found a solution:
http://west-wind.com/weblog/posts/5127.aspx
Thanks, Rick Strahl.
All I had to change was:
ControlUtils
.PopulateDD(this.Master.FindControl("ContentPlaceHolder1"), dropDowns);and also the signature of the method so it accepts all Control objects and not just Page objects:
public
static void PopulateDD(Control container, List<string> dropDowns)singhshash@g...
Member
2 Points
1 Post
Re: FindControl() doesn't work if page has MasterPage
Oct 17, 2007 02:53 PM|LINK
Brilliant Man, you saved me a lot of time with show me how to use master.findcontrol stuff. brilliant.....................
athornicroft
Member
7 Points
10 Posts
Re: FindControl() doesn't work if page has MasterPage
Feb 18, 2010 02:05 PM|LINK
Hi i have also had a problem with findcontrol with master pages, i found this thread useful
lblData.Text = ((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolderBody").FindControl("txtData")).Text;
s0uth
Member
2 Points
1 Post
Re: FindControl() doesn't work if page has MasterPage
Jun 08, 2010 04:04 PM|LINK
Dim mpContentPlaceHolder As ContentPlaceHolder Dim mpTextBox As TextBox mpContentPlaceHolder = _ CType(Master.FindControl("ContentPlaceHolder1"),ContentPlaceHolder) If Not mpContentPlaceHolder Is Nothing Then mpTextBox = CType(mpContentPlaceHolder.FindControl("TextBox1"), TextBox) If Not mpTextBox Is Nothing Then mpTextBox.Text = "TextBox found!" End If End If