如何在没有选择按钮的情况下在 GridView 中实现全行选择? [英] How to implement full row selecting in GridView without select button?

查看:19
本文介绍了如何在没有选择按钮的情况下在 GridView 中实现全行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing a feature that when the user press on any point in the row in a GridView the row will be selected instead of Select button.

为了实现这一点,我使用了以下代码:

To implement that, I'm using the following code:

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

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

With the code above, there are the following problems:

  • 只有当我将页面的 EnableEventValidation 设置为 false 时,这才能正常工作.
  • SelectedIndexChanged 仅在 Grid.DataBind() 在页面的 Page_Load 中被调用时才会被触发(在每次回发中).
  • This works fine only if I EnableEventValidation for the page is set to false.
  • The SelectedIndexChanged is only fired just in case the Grid.DataBind() is called in Page_Load for the page (in every postback).

我做错了吗?有没有更好的实现?

Am I doing something wrong? Is there a better implementation?

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

When EnableEventValidation is set to true, the following error will appear:

无效的回发或回调参数.事件验证在页面中使用配置或 <%@ Page EnableEventValidation="true" %> 启用.出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证.

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.

推荐答案

您必须在每次回发时添加此项,而不仅仅是在数据绑定时添加.因此,您应该使用 RowCreated-GridView 的事件.

You must add this on every postback and not only on databinding. Therefore you should use the RowCreated-Event of the 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

这篇关于如何在没有选择按钮的情况下在 GridView 中实现全行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆