2011-06-06 45 views
30

我正在实现一个功能,当用户按下GridView中该行的任意点时,该行将被选中而不是Select按钮。如何在没有选择按钮的情况下在GridView中实现全行选择?

enter image description here

要实现的是,我使用下面的代码:

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,会出现以下错误:

无效的回发或回调参数。事件验证在配置中启用,或在页面中启用<%@ Page EnableEventValidation =“true”%>。为了安全起见,此功能验证回发或回调事件的参数来自最初呈现它们的服务器控件。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法为注册回发或回调数据进行验证。

回答

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。网):

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 
+0

这并不能解决将EnableEventValidation设置为false的问题。 – 2011-06-06 10:50:40

+0

@ Tim:我编辑了这个问题,请检查一下。 – Homam 2011-06-06 11:13:43

+0

@Homam:你有没有使用我的代码,你仍然得到“无效的回发/回调参数” - 错误?我无法在我的项目中重现此错误,但可能还有其他原因。我现在还没有时间进一步调查。您可以使用[ClientScriptManager.RegisterForEventValidation](http://msdn.microsoft.com/zh-cn/library/ms223397.aspx)来避免它。也许[这个链接](http://www.codeproject.com/KB/webforms/DoubleClickGridviewRow.aspx)有帮助。 – 2011-06-06 11:19:58

-2

尝试使用在你的ASPX <tr>代码<asp:LinkButton>,设置LinkBut​​ton的CommandName='Select'而在项命令事件,处理该命令,并设置所选行的风格!

+1

你确定具有TD之间的linikbutton和TR产生一个有效的完全兼容HTML? – 2011-06-06 10:45:30

+0

@Davide Piras:在标准问题中没有提到符合标准的HTML!如果这是一项要求,那么不要接受解决方案!我出现了一些可能性! – Nauman 2011-06-06 12:13:35

+0

我不同意,我没有投票给你,并且不会,但即使没有在原始文章中提到,来吧我们在2011年夏季(几乎),让我们给跨浏览器和完全合规的答案:) ;-) – 2011-06-06 12:16:43

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> 
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 = “假” 但在这里你必须执行你已经在代码中完成的两件事小号 集EnableEventValidation虚假和网格数据绑定在页面加载..

11

而不是做它RowCreated的,你可以做到这一点的Render()。这样你就可以使用GetPostBackClientHyperlink的超载,在registerForEventValidation上使用true,并避免“无效的回发/回调参数”错误。

事情是这样的:

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); 
    } 
+0

谢谢你,这是我需要:) – 123iamking 2015-12-07 10:16:22

相关问题