我正在实现一个功能,当用户按下GridView中该行的任意点时,该行将被选中而不是Select按钮。如何在没有选择按钮的情况下在GridView中实现全行选择?
要实现的是,我使用下面的代码:
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
仅在Page_Load
页面(每次回发)中调用Grid.DataBind()
时才被触发。
我做错了什么?有更好的实施吗?
编辑: 当EnableEventValidation
设置为true
,会出现以下错误:
无效的回发或回调参数。事件验证在配置中启用,或在页面中启用<%@ Page EnableEventValidation =“true”%>。为了安全起见,此功能验证回发或回调事件的参数来自最初呈现它们的服务器控件。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法为注册回发或回调数据进行验证。
这并不能解决将EnableEventValidation设置为false的问题。 – 2011-06-06 10:50:40
@ Tim:我编辑了这个问题,请检查一下。 – Homam 2011-06-06 11:13:43
@Homam:你有没有使用我的代码,你仍然得到“无效的回发/回调参数” - 错误?我无法在我的项目中重现此错误,但可能还有其他原因。我现在还没有时间进一步调查。您可以使用[ClientScriptManager.RegisterForEventValidation](http://msdn.microsoft.com/zh-cn/library/ms223397.aspx)来避免它。也许[这个链接](http://www.codeproject.com/KB/webforms/DoubleClickGridviewRow.aspx)有帮助。 – 2011-06-06 11:19:58