r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink((Control)this.gridView, "Select$" + r.RowIndex);
您好,我使用上面的代码在 Asp.net 中的 GridView 中捕获一行上的点击事件。这段代码有效,我能够在 OnSelectdIndexChanged 中捕获新选择的行,但是回发也会导致整页刷新。
有什么方法可以取消页面刷新,但仍然更改所选行而不添加“选择”按钮?
最佳答案
您将需要 GridView 中的 RowCreated 事件来执行此操作。请参阅下面的示例:
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
您可以查看此链接了解更多关于不使用选择按钮选择行的信息 - How to implement full row selecting in GridView without select button?
为了防止 PostBack,我建议使用我之前评论然后删除的 UpdatePanel。下面是将 GridView 放入 UpdatePanel 的示例:
<asp:ScriptManager ID=" ScriptManager1" runat="server"/>
<asp:UpdatePanel ID=" UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
这是一些代码。所以我在评论部分分享了一个链接。请检查。
关于c# - 如何取消Page.ClientScript.GetPostBackClientHyperlink引起的页面刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973152/