Nelitza Martinez Nelitza Martinez -4 years ago 83
ASP.NET (C#) Question

Link button is not being disabled, how to disable link button in grid-view?

I have a Gridview with a link button in the first column. Once the link button is clicked I want to open a window, but I also want to disable the link button.

Not working
Once I click on the link button the window behavior is the expected one. However, the link button is not disabled. Therefore is permitting me to click on it over and over again.

The front end is:

<asp:LinkButton runat="server" ID="lnkbtnView" CommandArgument='<%# Eval("Id")%>' OnCommand="GetViewOnClientClick" >View<br/></asp:LinkButton>


GetviewonClientClick method:

protected void GetViewOnClientClick(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
if (row != null)
{
int index = row.RowIndex;

LinkButton link = (LinkButton)row.FindControl("lnkbtnView");
link.Enabled = false;
}
}


Why is not working as expected?
That's what I want to solve. I suspect I might have to do a rebinding or something related, but I don't quite understand what is really going on. Therefore, I don't know how to implement it.

Answer Source

I never found the solution to edit the control in that method. What I did instead was to read on the cycles of the grid view events.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events(v=vs.110).aspx

If you read the previous link you'll know that once you click on a record is going to trigger the RowDataBound event. Therefore; there you'll have access to the control and apply logic to edit the control's property.

Therefore because I have two types of records in this grid view I needed to store and to make persistence what records were clicked.

This is my fix to keep track of the clicked row:

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            gvs.SetHeaderArrows(e);
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int Id = (int)(e.Row.RowIndex);
            int? inspID = convert.ToIntQ(DataBinder.Eval(e.Row.DataItem, "InspectionID"));
            string rowclicked = string.Format("clickedrow{0}", Id);

            if (convert.ToIntQ(Session[rowclicked]) != null)
            {

                if (Id == Convert.ToInt32(Session[rowclicked]))
                {
                    LinkButton button = (LinkButton) e.Row.FindControl("lnkbtnView");
                    button.ForeColor = Color.Gray;
                    button.Enabled = false;

                }

                else
                {
                    LinkButton button = (LinkButton)e.Row.FindControl("lnkbtnView");
                    button.ForeColor = Color.DarkBlue;
                    button.Enabled = true;

                }

            }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download