FindControl() doesn't work if page has MasterPage

Last post 10-17-2007 10:53 AM by singhshash@gmail.com. 4 replies.

Sort Posts:

  • FindControl() doesn't work if page has MasterPage

    08-22-2006, 5:30 PM

    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

  • Re: FindControl() doesn't work if page has MasterPage

    08-22-2006, 11:19 PM

    Use Page.Master.FindControl instead.

     

  • Re: FindControl() doesn't work if page has MasterPage

    08-23-2006, 6:53 AM

    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?

  • Re: FindControl() doesn't work if page has MasterPage

    08-23-2006, 2:16 PM

    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)
  • Re: FindControl() doesn't work if page has MasterPage

    10-17-2007, 10:53 AM

    Brilliant Man, you saved me a lot of time with show me how to use master.findcontrol stuff. brilliant.....................

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter