views:

591

answers:

3

Hi Guys,

How do i get access to a control (linkbutton in my case) that is within the itemtemplate section of asp datalist control? For example: I want to set the linkbutton to visible false, but cannot figure out how to get the reference to it from the code-behind.

Example code:

<asp:datalist id="datalist1" runat="server">
<ItemTemplate>
   <asp:label id="label1" runat="server"></asp:label>
   <asp:linkbutton id="editButton" runat="server" text="Edit"></asp:linkbutton>
</ItemTemplate>
</asp:datalist>

Thanks.

+2  A: 

you need to handle item data bound event. Then find the linkbutton and set its properties there like this:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
     LinkButton lb=e.Item.FindControl("editButton") as LinkButton;
     if(lb!=null){
         if(some condition){
            lb.Visible=false;
         }
     }
}

EDIT:- you can get more information regarding customizing data list at runtime here

A: 

You might not even need the code-behind if you do something like this

<asp:datalist id="datalist1" runat="server">
<ItemTemplate>
   <asp:label id="label1" runat="server"></asp:label>
   <asp:linkbutton id="editButton" runat="server" text="Edit" Visible='<%# Eval("SomeBooleanDataElement") %>'></asp:linkbutton>
</ItemTemplate>
</asp:datalist>
JNappi
A: 

Thank you sir..