C# GridView 行单击

19
当我点击GridView中的一行时,我想要跳转到另一页,并且携带从数据库得到的ID。

在我的RowCreated事件中,我有以下代码行:
e.Row.Attributes.Add(
     "onClick",
     ClientScript.GetPostBackClientHyperlink(
          this.grdSearchResults, "Select$" + e.Row.RowIndex));

为了防止错误消息,我有以下代码:

protected override void Render(HtmlTextWriter writer)
{
    // .NET will refuse to accept "unknown" postbacks for security reasons. 
    // Because of this we have to register all possible callbacks
    // This must be done in Render, hence the override
    for (int i = 0; i < grdSearchResults.Rows.Count; i++)
    {
        Page.ClientScript.RegisterForEventValidation(
                new System.Web.UI.PostBackOptions(
                    grdSearchResults, "Select$" + i.ToString()));
    }
    // Do the standard rendering stuff
    base.Render(writer);
}

我该如何为一行数据赋予唯一的ID(从数据库中获取),并使得当我点击这一行时,另一个页面可以像点击href链接一样被打开,同时该页面可以读取这个ID。

9个回答

20

Martijn,

这里有另一个示例,其中包含一些巧妙的行高亮和href样式光标:

protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
    e.Row.Attributes.Add("style", "cursor:pointer;");
    e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
  }
}

上面的代码适用于.NET 3.5。然而,如果将id列设置为Visible="false",则会得到您的id键的空查询字符串值:

<asp:GridView ID="gvSearch" runat="server" OnRowDataBound="gvSearch_RowDataBound" AutoGenerateColumns="false">
  <Columns>
    <asp:BoundField DataField="id" Visible="false" />
    <asp:BoundField DataField="first_name" HeaderText="First" />
    <asp:BoundField DataField="last_name" HeaderText="Last" />
    <asp:BoundField DataField="email" HeaderText="Email" />
    <asp:BoundField DataField="state_name" HeaderText="State" />
  </Columns>
</asp:GridView>

因此,将第一列更改为以下内容:

<asp:BoundField DataField="id" ItemStyle-CssClass="hide" />

在页面顶部添加以下CSS:

<head>
  <style type="text/css">
    .hide{
      display:none;
    }
  </style>
<head>

如果要隐藏表头的第一列,请在后台代码gvSearch_RowDataBound()中添加以下内容:

if (e.Row.RowType == DataControlRowType.Header)
{
  e.Row.Cells[0].CssClass = "hide";
}

显然,您也可以在后台代码中隐藏id列,但这会导致标记中出现更多文本而不是CSS类:

e.Row.Cells[0].Attributes.Add("style", "display:none;");
e.Row.Attributes.Add("style", "cursor:pointer;");


这是一个完整的解决方案:)其中包括HTML/Code behind代码! - Prashant Pimpale

19

我有解决方案。

这是我所做的:

if(e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}

我已经将前面的代码放在了RowDataBound事件中。


2
使用Id转换e.Row.DataItem,Eval操作较慢,并且不能让重构函数意识到您已更改了ID字段的名称。 - Andrew Bullock

3
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
        e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";    
    }
}

1
通过JS导航不好,如果不是内部网站。谷歌不会索引此内容。 - Arnis Lapsa

1
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)  
{     
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow gvr = e.Row;
            string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();         
            gvr.Attributes.Add("OnClick", "javascript:location.href='Default.aspx?id=" + abc + "'");
        }         

   }  
}  

1
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
        string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString(); 
        e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";     
    } 
} 

0

JohnB,你的代码非常好用,我只是加了一个小技巧来避免鼠标移出后交替行样式被破坏。

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");

已更改为:

e.Row.Attributes.Add("onmouseout", "if(" + e.Row.RowIndex + "% 2 == 0) { this.style.backgroundColor=''; } else { this.style.backgroundColor = '#E8F7EA'; }");

如果有更好的方法,请告诉我,但这对我来说已经完美运作了。
问候。

0

你的ID是否可以与在GridView中显示的数据项相关联?

如果可以,你可以使用e.Row.DataItem,并将其转换为相应的类型。


0
你可以使用 GridView 的 RowCommand 事件来实现。在你想要设置点击的按钮/链接中,设置 CommandName 和 CommandArgument,在事件方法的 EventArgs 参数中可以访问它们。

0

在网格视图中单击行会重定向到其他页面

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
             e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
        }
    }

运行得非常好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接