我正在实现一项功能,当用户按下 GridView 中行中的任何点时,将选择该行而不是 Select 按钮。
为了实现这一点,我使用以下代码:
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
时,会出现以下错误:
无效的回发或回调参数。使用配置或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。