如何实现在GridView控件整行选择不选择按钮? [英] How to implement full row selecting in GridView without select button?
问题描述
我实现一个功能,当在GridView行中的任意一点,用户preSS行将被改为选择选择按钮的。
要实现的是,我用下面的code:
保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{
//设置鼠标光标所选择的行。
e.Row.Attributes.Add(的onmouseover,this.style.cursor ='手';); //该seelctButton存在用于确保选择功能
//并与相应的事件hanlder绑定。
LinkButton的selectButton =新的LinkButton()
{
的CommandName =选择,
文本= e.Row.Cells [0]。文本
}; e.Row.Cells [0] .Controls.Add(selectButton);
e.Row.Attributes [的OnClick] =
Page.ClientScript.GetPostBackClientHyperlink(selectButton,);
}
}
通过上面的code,存在以下问题:
- 这工作正常,只有当我
EnableEventValidation
为页面设置为假
。 - 的
的SelectedIndexChanged
才刚刚在的情况下解雇了Grid.DataBind()
被称为的Page_Load
的页面(在每个回发)。
我是不是做错了什么?有没有更好的实现?
编辑:
当 EnableEventValidation
设置为真正
,会出现以下错误:
无效的回发或回调参数。在一个页%@页EnableEventValidation =真%>;事件验证正在使用中配置或所述启用。为了安全起见,此功能验证参数回发或回调事件,从最初呈现这些事件的服务器控件。如果数据是有效的和预期,使用ClientScriptManager.RegisterForEventValidation方法来注册回发或回调数据以进行验证。
块引用>解决方案您必须在每次回传添加此不仅在数据绑定。因此,你应该使用RowCreated-Event的GridView控件。
例如
(C#):
保护无效GridView1_RowCreated(对象发件人,System.Web.UI.WebControls.GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow){
e.Row.Attributes [的onmouseover] =this.style.cursor ='指针'; this.style.textDecoration ='底线';;
e.Row.Attributes [的onmouseout] =this.style.textDecoration =无;
e.Row.ToolTip =点击选择行;
e.Row.Attributes [的onclick] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1,选择$+ e.Row.RowIndex);
}
}(VB.Net):
私人小组GridView1_RowCreated(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewRowEventArgs)处理GridView1.RowCreated
如果e.Row.RowType = DataControlRowType.DataRow然后
e.Row.Attributes(的onmouseover)=this.style.cursor ='指针'; this.style.textDecoration ='底线';
e.Row.Attributes(的onmouseout)=this.style.textDecoration =无;
e.Row.ToolTip =点击选择行
e.Row.Attributes(onclick事件)= Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1,选择$&放大器; e.Row.RowIndex)
万一
结束小组I'm implementing a feature that when the user press on any point in the row in a GridView the row will be selected instead of Select button.
To implement that, I'm using the following code:
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, ""); } }
With the code above, there are the following problems:
- This works fine only if I
EnableEventValidation
for the page is set tofalse
.- The
SelectedIndexChanged
is only fired just in case theGrid.DataBind()
is called inPage_Load
for the page (in every postback).Am I doing something wrong? Is there a better implementation?
Edit: When
EnableEventValidation
is set totrue
, the following error will appear: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.
解决方案You must add this on every postback and not only on databinding. Therefore you should use the RowCreated-Event of the GridView.
For example
(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
这篇关于如何实现在GridView控件整行选择不选择按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!