How to access items from datalist

How to access Label5 from behind the records file (default.aspx.cs)?

         <ItemTemplate>

             <asp:Image ID="Image1" runat="server" />
        <asp:HyperLink ID="HyperLink1" CssClass="nav_url_odg" runat="server"><%# Eval("user") %></asp:HyperLink> <span class="odgovorio">je odgovorio:</span><br />
       <div>
           <asp:Label ID="Label5" runat="server" Text=""></asp:Label></div>
    </div>
    <br /></div> </div>
      </ItemTemplate>

        </asp:DataList>

      

+2


a source to share


1 answer


Add an OnItemDataBound event handler to your markup for the DataList:

<asp:DataList runat="server" OnItemDataBound="datalist_ItemDataBound">

      



Then, in your code behind you can check the type of the element and find the label control by its ID:

    void datalist_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            Label label5 = (Label)e.Item.FindControl("label5");
            // now you have access for each bound row
        }
    }

      

+4


a source







All Articles