Home > Software engineering >  How to disable hyperlink on LnkButton?
How to disable hyperlink on LnkButton?

Time:09-16

I have a table of data. One of the fields is UName. UName has hyperlink. I want to disable it in SOME rows.

I have two LnkButtons in ItemTemplate. When I remove first LnkButton in ItemTemplate, hyperlink from UName field is removed. Since I do not want to disable it for each row, I want to get a way to disable hyperlink from CodeBehind.

<telerik:GridTemplateColumn DataField="UName" GroupByExpression="UName" UniqueName="UName"
                                            InitializeTemplatesFirst="false" HeaderText="Name" CurrentFilterFunction="Contains">
                                            <HeaderStyle />
                                            <HeaderTemplate>
                                                <span onm ouseover='ShowColumnHeaderMenu(event,"UName")'>Name
                                                </span>
                                                <asp:Image ID="imgHeader1" ImageUrl="" Visible="false" runat="server" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <LnkButton:LnkButton ID="HypUName" runat="server" style="white-space: nowrap" Text='<%# Server.HtmlEncode(Eval("UName").ToString()) %>'></LnkButton:LnkButton>
                                                 <LnkButton:LnkButton ID="LnkButton1" runat="server" Text='<%# Eval("UName").ToString() %>' Menuid="5"></LnkButton:LnkButton>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>

In summary, my goal is to disable LnkButton with ID "HypUName" from CodeBehind.

CodePudding user response:

You need to loop through find the control and disable it, if it passes what ever condition you are looking for. example below

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
 {
   // look for your control  here and disable it if needed
    LinkButton lnk= (LinkButton)e.Row.FindControl("HypUName");
    
     if(lnk != null)
     {
       // do your condition check here
        //if passes disable link 
       lnk.Enabled = false;
     }
  }
}
  • Related