开发者

can't access controls inside <% %> (code tags) inside repeater's ItemTemplate

开发者 https://www.devze.com 2023-04-03 04:42 出处:网络
without the repeater this works, but writing this inside repeater it\'s just compile error <asp:Repeater runat=\"server\" ID=\"rep1\">

without the repeater this works, but writing this inside repeater it's just compile error

<asp:Repeater runat="server" ID="rep1">
    <ItemTemplate>
      <li>
          <o:TextBox runat="server" ID="txtLastName" />
<% 
//this doesn't works outside of the repeater but here it doesn't
txtName.Text = txtLastName.ClientID;
%>    开发者_JAVA技巧                           
          <o:TextBox runat="server" ID="txtName"  />
       </li>
     </ItemTemplate>
</asp:Repeater>


You can only use DataBind syntax inside a ItemTemplate <%# %>. If you want to refer to another control use the NamingContainer.

<asp:Repeater runat="server" ID="rep1">
    <ItemTemplate>
      <li>
          <o:TextBox runat="server" ID="txtLastName" />
          <%# Container.FindControl("txtLastName").ClientID %>    
          <o:TextBox runat="server" ID="txtName"  />
       </li>
     </ItemTemplate>
</asp:Repeater>

Otherwise you can always hook ItemDataBound to manipulate the control on the server-side.

protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{       
   if (e.Item.ItemType == ListItemType.Item 
                || e.Item.ItemType == ListItemType.AlternatingItem)
   {
       var txtLastName = e.Item.FindControl("txtLastName") as TextBox;
       var txtName = e.Item.FindControl("txtName") as TextBox;

       ...
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消