我正在实现一项功能,当用户按下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
仅在Grid.DataBind()
为页面调用Page_Load
时才会被解雇(在每次回发中)。我做错了吗?有更好的实施吗?
修改
当EnableEventValidation
设置为true
时,将显示以下错误:
无效的回发或回调参数。使用配置或<%@ Page EnableEventValidation =“true”%>启用事件验证在一个页面中。出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。
答案 0 :(得分:46)
您必须在每次回发时添加此项,而不仅仅是数据绑定。因此,您应该使用GridView的RowCreated - 事件。
例如
(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);
}
}
(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
答案 1 :(得分:13)
而不是在RowCreated
上执行,您可以在Render()
上执行此操作。这样你就可以在GetPostBackClientHyperlink
上使用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);
}
答案 2 :(得分:3)
<style type="text/css">
.hiddenColumn
{
display: none;
}
.rowGrid
{
cursor: pointer;
}
</style>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="true" >
<RowStyle CssClass="rowGrid" />
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="true" HeaderStyle-CssClass="hiddenColumn"
ItemStyle-CssClass="hiddenColumn" FooterStyle-CssClass="hiddenColumn" />
</Columns>
</asp:GridView>
<script type="text/javascript">
$(function () {
$("#<%= GridView1.ClientID %> tr.rowGrid")
.live("click", function (event) {
$(this).find("input[type='button'][value='Select']").click();
});
$("#<%= GridView1.ClientID %> input[type='button'][value='Select']")
.live("click", function (event) {
event.stopPropagation();
});
});
</script>
答案 3 :(得分:0)
试试这个 在网格中添加OnSelectedIndexChanged事件
OnSelectedIndexChanged="Grid_SelectedIndexChanged"
然后是代码
protected void Grid_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvSummary.SelectedRow;
//Int32 myvalue= Convert.ToInt32(row.Attributes["ColumnName"].ToString());
}
并设置EnableViewState =“false” 但是你必须在你的代码中完成另外两件事 将EnableEventValidation设置为false,并在页面加载..
上设置网格数据绑定答案 4 :(得分:-2)
尝试在ASPX代码的<asp:LinkButton>
周围使用<tr>
,设置LinkButton的CommandName='Select'
并在项目命令事件中处理此命令并设置所选行的样式!