【发布时间】:2011-09-09 04:23:07
【问题描述】:
我正在实现一项功能,当用户按下 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时,会出现如下错误:
回发或回调参数无效。使用配置或页面中的 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。
【问题讨论】:
标签: c# asp.net gridview selectedindexchanged