【问题标题】:c# gridview row clickc# gridview 行点击
【发布时间】:2010-09-24 18:41:26
【问题描述】:

当我单击 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(来自 DB),当我点击该行时,另一个页面会打开(比如点击一个 href)并且该页面可以读取 ID。

【问题讨论】:

    标签: c# gridview click


    【解决方案1】:

    马丁,

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

    所以将第一列改为:

    &lt;asp:BoundField DataField="id" ItemStyle-CssClass="hide" /&gt;

    将此 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/代码!
    【解决方案2】:

    我有办法。

    这就是我所做的:

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

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

    【讨论】:

    • Id cast e.Row.DataItem, Eval 很慢,并且不允许重构来发现您可能已经更改了 ID 字段名称的事实
    【解决方案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 + "'";    
        }
    }
    

    【讨论】:

    • 如果不是内部站点,通过 JS 导航是不好的。 Google 不会将其编入索引。
    【解决方案4】:
    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 + "'";     
        } 
    } 
    

    【讨论】:

      【解决方案5】:
      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 + "'");
              }         
      
         }  
      }  
      

      【讨论】:

        【解决方案6】:

        你的ID可以和gridview中显示的数据项相关吗?

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

            网格视图中的行点击重定向到其他页面

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

            效果很好

            【讨论】:

              【解决方案9】:

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

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

              您好。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-31
                • 1970-01-01
                • 2016-06-24
                • 2023-04-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多