Question

I am trying to find out if it's possible to have a control inside another control in ASP, like this:

 <asp:FormView ID="FormView1" runat="server" Width="630px" Height="496px">
   <ItemTemplate>
       <asp:Literal ID="ID" runat="server">Idnumber: </asp:Literal><%#Eval("ID") %>
             <asp:DataList ID="DataList1" runat="server">
                        <ItemTemplate>
                            <asp:HyperLink ID="ID" runat="server"><a href="url/<%# Eval("ID") %>/<%#Eval("FILE")%>"> <%# Eval("FILE") %> </a></asp:HyperLink>
                        </ItemTemplate>
             </asp:DataList>
   </ItemTemplate>
  </asp:FormView>

Can I access DataList1 control? I have been trying, but I can't figure it out, I should be able to access nested controls, but I cant get it to do it.

Was it helpful?

Solution

Controls inside Template tags cannot be directly accessed in code behind. Instead you should use FindControl method:

var dataList1 = (DataList)FormView1.FindControl("DataList1");

Note that this might not work in early stages of page life cycle (not until Page_Load I believe).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top