c#gridview行点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/331231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:30:16  来源:igfitidea点击:

c# gridview row click

c#gridviewclick

提问by Martijn

When I click on a row in my GridView, I want to go to a other page with the ID I get from the database.

当我单击 GridView 中的一行时,我想使用从数据库中获取的 ID 转到另一个页面。

In my RowCreated event I have the following line:

在我的 RowCreated 事件中,我有以下几行:

e.Row.Attributes.Add(
     "onClick",
     ClientScript.GetPostBackClientHyperlink(
          this.grdSearchResults, "Select$" + e.Row.RowIndex));

To prevent error messages i have this code:

为了防止错误消息,我有这个代码:

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

How can I give a row a unique ID (from the DB) and when I click the row, another page is opened (like clicking on a href) and that page can read the ID.

我怎样才能给一行一个唯一的 ID(来自数据库),当我点击该行时,另一个页面被打开(比如点击一个 href)并且该页面可以读取该 ID。

采纳答案by Andrew Bullock

I have the solution.

我有解决方案。

This is what i have done:

这是我所做的:

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

I have putted the preceding code in the RowDataBound event.

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

回答by Andrew Bullock

Can your ID be related to the data item displayed in the gridview?

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

If so you can use e.Row.DataItem, and cast it to whatever type it is.

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

回答by Andrew Bullock

row click in grid view redirect to other page

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

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

works absolutely fine

工作得很好

回答by Andrew Bullock

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

回答by Bhaskar

You can use the RowCommand event of the grid view for it. In your button/link where you wnat to set the click on, set the CommandName and CommandArgument, which you can accees at the EventArgs paramter of the event method.

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

回答by JohnB

Martijn,

马丁,

Here's another example with some nifty row highlighting and a href style cursor:

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

The code above works in .NET 3.5. However, you can't set your id column to Visible="false" because you'll get a blank query string value for your id key:

上面的代码适用于 .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>

So change the first column to this instead:

因此,将第一列更改为此:

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

Add this css to the top of your page:

将此 css 添加到页面顶部:

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

But to hide the first cell of your header row, add this to your gvSearch_RowDataBound() in code-behind:

但是要隐藏标题行的第一个单元格,请将其添加到代码隐藏中的 gvSearch_RowDataBound() 中:

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

Obviously, you could have hidden the id column in code-behind too, but this will result in more text in your markup than a css class:

显然,您也可以在代码隐藏中隐藏 id 列,但这将导致标记中的文本比 css 类更多:

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

回答by mohan

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

回答by sh4

JohnB, Your code works very fine, i added just a little hack to avoid alternatingRowStyle ruining after mouseout.

JohnB,你的代码工作得很好,我添加了一些小技巧以避免在鼠标移开后alteralRowStyle 损坏。

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

Changed to:

变成:

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

If there's a better way to do it, please let me know, but it's working perfect for me.

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

Greetings.

你好。

回答by Jyoti Nagda

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

   }  
}