0

I've created a base Page class which has some default functionality for adding controls to a div on the page. This page is named BasePage.

public abstract class BasePage : System.Web.UI.Page
{
    public String FormId;
    public BasePage() : this("EntityForm")
    {            
    }

    public BasePage(String formId)
    {
        this.FormId = formId;
    }       

    protected virtual void Page_LoadComplete(object sender, EventArgs e)
    {
        this.FindControl(this.FormId); // returns null;
    }
}

// Derived class
public partial class _default : BasePage
{
    public _default("test")
    {
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        //base.Page_Load(sender, e);
    }
}

// ASPX code
<asp:Content ID="Content1" ContentPlaceHolderID="cphHead" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphCenter" runat="server">
    <div id="test" runat="server" class="frmData">    
    </div>    
</asp:Content>

I've got the same functionality working for pages without having a masterpagefile connected to it, it might have something to do with this.. What am i missing here?

DatRid
  • 1,169
  • 2
  • 21
  • 46
Mark Rijsmus
  • 627
  • 5
  • 16

1 Answers1

1

Use a recursive FindControl extension method to find the control:

C#, FindControl

private Control FindControlRecursive(Control ctrl, string id)
{
    if(ctrl.ID == id)
    {
        return ctrl;
    }
    foreach (Control child in ctrl.Controls) 
    { 
        Control t = FindControlRecursive(child, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 
    return null;
}
Community
  • 1
  • 1
E. van der Spoel
  • 260
  • 1
  • 15