Hom*_*mam 30 c# asp.net gridview selectedindexchanged
我正在实现一个功能,当用户按下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, "");
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,存在以下问题:
EnableEventValidation将页面设置为时,这才能正常工作false.SelectedIndexChanged仅触发公正的情况下Grid.DataBind()被调用Page_Load的页面(在每个回发).难道我做错了什么?有更好的实施吗?
编辑:
当EnableEventValidation设置true为时,将出现以下错误:
无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证.
Tim*_*ter 46
您必须在每次回发时添加此项,而不仅仅是在数据绑定上.因此,您应该使用GridView 的RowCreated -Event.
例如
(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);
}
}
Run Code Online (Sandbox Code Playgroud)
(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
Run Code Online (Sandbox Code Playgroud)
ale*_*bog 13
而不是这样做RowCreated,你可以做到Render().这样你可以使用GetPostBackClientHyperlink带有true 的重载registerForEventValidation并避免"无效的回发/回调参数"错误.
像这样的东西:
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
r.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
r.Attributes["onmouseout"] = "this.style.textDecoration='none';";
r.ToolTip = "Click to select row";
r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + r.RowIndex,true);
}
}
base.Render(writer);
}
Run Code Online (Sandbox Code Playgroud)