简体   繁体   中英

Disable click event over GridView ASP.NET

I'm working on ASP.NET with a GridView that I made clickable with this method:

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        GridView gr = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.background='#3260a0';;this.style.color='white'";
            e.Row.Attributes["onmouseout"]  = "this.style.textDecoration='none';this.style.background='white';this.style.color='black'";
            e.Row.Attributes["onclick"]     = Page.ClientScript.GetPostBackClientHyperlink(RH, "Select$" + e.Row.RowIndex);
            e.Row.Attributes["style"]       = "cursor:pointer";
        }           
    }

Now

I want to disable that functionality when I press another button

, however I don't know how to. I tried using Enable = false, but aparently it doesn't affect the script and it keeps allowing me to click over the grid again. What I want is that when I press the button to disable the grid, it will stop the mouse over animation and the onclick immediatly, but haven´t found a proper way.

Here is the code of the GridView just in case:

<asp:GridView ID="RH" runat ="server" margin-right ="auto" 
         margin-left="auto" OnSelectedIndexChanged="RH_SelectedIndexChanged"
         OnRowDataBound ="OnRowDataBound" CssClass ="GridView" HorizontalAlign="Center" 
         AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="5">            
</asp:GridView>

Let you have a button for Disable:

<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="Disable Click" />

Then on button click we can remove onclick evented added dynamically:

 protected void btn_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in RH.Rows)
            {
                row.Attributes.Remove("onclick");
            }
        }

If you need onclick event again add that attribute with another button click:

 protected void btn2_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in RH.Rows)
            {
                row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(RH, "Select$" + row.RowIndex);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM