禁用gridviewasp.net上的单击事件

本文关键字:单击 事件 gridviewasp net 禁用 | 更新日期: 2023-09-27 18:17:43

我在做ASP。. NET中有一个GridView,我用这个方法使它可以点击:

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";
        }           
    }
现在

我想在按下另一个按钮时禁用此功能

,然而我不知道如何。我尝试使用Enable = false,但显然它不影响脚本,它一直允许我再次点击网格。我想要的是,当我按下按钮禁用网格时,它会立即停止鼠标在动画和onclick上,但还没有找到合适的方法。

下面是GridView的代码,以防万一:

<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>

禁用gridviewasp.net上的单击事件

让你有一个禁用按钮:

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

然后点击按钮,我们可以删除onclick事件动态添加:

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

如果您需要onclick事件再次添加该属性与另一个按钮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);
            }
        }