gpt4 book ai didi

c# - 如何在没有选择按钮的情况下在 GridView 中实现全行选择?

转载 作者:IT王子 更新时间:2023-10-29 04:10:46 26 4
gpt4 key购买 nike

我正在实现一项功能,当用户按下 GridView 中行中的任意点时,将选择该行而不是选择按钮。

enter image description here

为了实现它,我使用了以下代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Set the hand mouse cursor for the selected row.
e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

// The seelctButton exists for ensuring the selection functionality
// and bind it with the appropriate event hanlder.
LinkButton selectButton = new LinkButton()
{
CommandName = "Select",
Text = e.Row.Cells[0].Text
};

e.Row.Cells[0].Controls.Add(selectButton);
e.Row.Attributes["OnClick"] =
Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
}
}

使用上面的代码,存在以下问题:

  • 仅当我将页面的 EnableEventValidation 设置为 false 时才能正常工作。
  • SelectedIndexChanged 仅在 Grid.DataBind() 在页面的 Page_Load 中调用(在每次回发中)时才会触发.

我做错了什么吗?有没有更好的实现方式?


编辑:EnableEventValidation设置为true时,会出现如下错误:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

最佳答案

您必须在每次回发时添加它,而不仅仅是在数据绑定(bind)上。因此你应该使用 RowCreated -GridView 的事件。

例如

(C#):

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);
}
}

(VB.Net):

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
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") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub

关于c# - 如何在没有选择按钮的情况下在 GridView 中实现全行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com