简体   繁体   中英

Unable to access a server cotrol on page_load method

I have place holder in my aspx page inside html table as below:

<div id="div1" runat="server" onmouseover="divexpand()" onmouseout="collapsediv()">
    <span id="sp"><i>Status:</i></span>
        <div id="div2" style="display:block;">
        <table style="padding:10px;">
            <tbody><tr>
                <td>A:</td>
                <td>
                    <asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
                </td>
                <td>
                    <asp:PlaceHolder ID="ph2" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
         </tbody>
        </table>
    </div>

and i'm trying to access the placeholders 'ph1' and 'ph2' on page_load method code behind using Page.FindControl(id) method as below:

protected void Page_Load(object sender, EventArgs e)
    {
        PlaceHolder ph1 = Page.FindControl("ph1") as PlaceHolder;
        PlaceHolder ph2 = Page.FindControl("ph2") as PlaceHolder;
        ph1.Controls.Add(new LiteralControl("<b><i>TEST</i></b>"));
        ph2.Controls.Add(new LiteralControl("<b><i>TEST</i></b>"));
    }

But I could not access the placeholders. Any help would be appreciated.

您应该在没有Page.FindControl的情况下从后面的代码中将控件添加到占位符

There is no need to use FindControl in this context. Simple access the PlaceHolder controls directly from code-behind using their respective IDs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM