简体   繁体   English

在模板用户控件中查找子控件

[英]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). 我需要能够在运行时设置litSite文字的文本(它会根据另一种方法更改)。 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. 控制器将其数据绑定到Page_Load中,因此您之后只能访问其控件。 Also, you don't have to use FindControl because the child controls are directly accessible. 另外,您不必使用FindControl,因为可以直接访问子控件。 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() %> 搜索<%#GetLabelText()%>

You would define the GetLabelText() function. 您将定义GetLabelText()函数。

There is some documentation here Ektron 8.5 SearchView 这里有一些文档Ektron 8.5 SearchView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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