C#中的GridView行点击 [英] c# gridview row click

查看:543
本文介绍了C#中的GridView行点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在GridView中点击一排,我想去一个其他页面的ID我从数据库中获取。

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.

在我RowCreated事件我有以下行:

In my RowCreated event I have the following line:

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

要prevent错误消息我有这个code:

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

我怎么可以给一个行具有唯一的ID(从DB),当我点击该行,另外一个页面被打开(如点击A HREF)和页面可以读取ID。

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.

推荐答案

我有解决方案。

这是我做了什么:

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

我在RowDataBound事件推的preceding code。

I have putted the preceding code in the RowDataBound event.

这篇关于C#中的GridView行点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆