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。

4

9 回答 9

20

马汀,

这是另一个带有一些漂亮的行突出显示和 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;");

于 2010-04-06T02:33:23.460 回答
19

我有解决办法。

这就是我所做的:

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

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

于 2008-12-01T18:01:32.147 回答
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 + "'";    
    }
}
于 2009-02-13T17:12:18.270 回答
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 + "'";     
    } 
} 
于 2010-07-07T14:01:28.313 回答
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 + "'");
        }         

   }  
}  
于 2011-06-15T09:46:31.653 回答
0

您的 ID 可以与 gridview 中显示的数据项相关吗?

如果是这样,您可以使用 e.Row.DataItem,并将其转换为任何类型。

于 2008-12-01T15:52:45.467 回答
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 + "'";
        }
    }

工作得很好

于 2009-02-13T17:10:50.647 回答
0

您可以为它使用网格视图的 RowCommand 事件。在您要设置单击的按钮/链接中,设置 CommandName 和 CommandArgument,您可以在事件方法的 EventArgs 参数中访问它们。

于 2009-06-22T14:14:48.373 回答
0

JohnB,您的代码工作得很好,我只添加了一点技巧,以避免在鼠标悬停后破坏 alterRowStyle。

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'; }");

如果有更好的方法,请告诉我,但它对我来说是完美的。

问候。

于 2011-03-03T08:37:42.940 回答