Search code examples
c#asp.netektron

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?


Solution

  • 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";
    }