我正在实现一个功能,当用户在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()
时(在每个postback中)才会触发。
我做错了什么吗?有更好的实现方法吗?
编辑:当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.