简体   繁体   中英

Finding a child control inside a templated usercontrol

    <cc1:SiteSearchInputView ID="ssInputView" ControllerID="ssController" runat="server">
    <ItemTemplate>
        <table border="0" cellspacing="0" cellpadding="0">
              <tr>
            <td><label>Search <asp:Literal ID="litSite" runat="server" /></label></td>
            <td><asp:TextBox ID="tbSearchText" runat="server" /></td>
            <td><asp:Button ID="btnSearch" CssClass="searchBTN" runat="server" /></td>
          </tr>
            </table>
    </ItemTemplate>
</cc1:SiteSearchInputView>

I need to be able to set the text for the litSite literal at runtime (it changes based on another method). When I try using

Literal l = (Literal) ssInputView.FindControl("litSite");

I get an "Object not set to instance of an object" error.

How do you set the value of a child control inside a templated user control when you don't have access to the source of the templated control?

If you don't have access to the source of the control, you have to access the control via mechanisms that they've designed for you. Check the documentation to see what it is they expose.

The controller binds its data in Page_Load so you can only access its controls after. Also, you don't have to use FindControl because the child controls are directly accessible. So this will work for you:

protected void Page_PreRender(object sender, EventArgs e)
{
    Literal1.Text = "Hello, World";
}

Instead of writing to the literal, have the label call a function to get its text. Inside your template you can call a function to get the text that you want during the databind.

Search <%# GetLabelText() %>

You would define the GetLabelText() function.

There is some documentation here Ektron 8.5 SearchView

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