This project is read-only.
1
Vote

This doesn't work when using Master page

description

... in which case the control can't be found with regular ID, as asp.net prefix was added automatically. Is there a workaround out there?

comments

mrleonhuang wrote May 18, 2016 at 3:31 AM

If you site.master.aspx is like this:

<body>
    <form id="form1" runat="server">
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>
</form>
</body>

And the page is like this:

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <asp:TextBox ID="ID" runat="server"></asp:TextBox>
    <asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>  
</asp:Content>

In your page.aspx.cs, you should use bind model to "ContentPlaceHolder" instead of "this":

ContentPlaceHolder holder= Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
ModelBinder.BindControl(YOUR_MODEL, holder);

Reference:
http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/control-id-naming-in-content-pages-cs