views:

1385

answers:

2

Hey Guys, I have this linkbutton within a datalist and I'm trying to get access to the datalist on the pageload so i can set the linkbutton to be enabled or not based on the user's role.

<asp:DataList id="dlRecommendations" runat="server" DataKeyField="Key" Width="900">
   <ItemTemplate>
      <asp:LinkButton id="lnkEdit" Text="Edit" Runat="server" CommandName="Edit">    
      </asp:LinkButton>
  </ItemTemplate>
</asp:DataList>

Within the page load I want to be able to access the linkbutton to enable or disable it based on the user's role.

 private void Page_Load(object sender, System.EventArgs e) {
  //perhaps something like this:
  lnkEdit.Enabled = false;
  ....
}
A: 

DataList is a databound control - it builds rows only when data is supplied. To access link inside row use ItemDataBound event and access e.Item.FindControl("linkId");

Dewfy
+1  A: 

I think you will be populating the datalist the first time page is loaded. So just wireup ItemDataBound, find link and disable it.

    void dlRecommendations_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var link = e.Item.FindControl("lnkEdit") as LinkButton;
        if (link != null)
        {
            link.Enabled = UserHasRight;//if user has right then enabled else disabled
        }
    }
TheVillageIdiot