在GridView中创建事件以动态创建超链接字段 [英] Creating Event to Dynamically created hyperlinkField in gridview

查看:145
本文介绍了在GridView中创建事件以动态创建超链接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在为gridview动态创建绑定字段和超链接字段.

因此,一旦绑定了数据,gridview就会显示包含绑定字段以及超链接的日期.
我的问题是,当我单击超链接字段时,它必须重定向到带有表的Identity值的其他页面.
我也曾尝试给出数据键名称,但无法访问它们

这是我的代码

Hi,

i am Dynamically creating bound fields and hyperlink field for the gridview.

so once the data is bound the gridview displays the day which contains bound field as well as hyperlink.
my problem is when i click on hyperlinkfield it has to redirect to the other page carrying the Identity value of the table
i have also tried giving datakey names but unable to access them

here is my code

if (item.Text == "CompanyName")
                 {
                     //string id = gdvCompanies.SelectedDataKey.Value.ToString();
                     HyperLinkField hl = new HyperLinkField();
                     //hl.Click = new EventHandler(hl_Click);
                     hl.DataTextField = item.Value;
                     hl.HeaderText = item.Value;
                     //hl.Click += new EventHandler(clickMe);
                     hl.NavigateUrl = "EditCompany.aspx?&CID={1}";

                     gdvCompanies.Columns.Add(hl);
                 }
                 else
                 {
                     BoundField b = new BoundField();
                     b.DataField = item.Value;
                     b.HeaderText = item.Value;
                     gdvCompanies.Columns.Add(b);
                 }





how can i over come this.

推荐答案

您使用Gridview的RowDataBound事件.

You use the RowDataBound event of your Gridview.

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    ...

    //get the DataItem
    DataRowView rowView = (DataRowView)e.Row.DataItem;
    if(!string.IsNullOrWhitespace(rowView["CID"].ToString())
        hl.NavigateUrl = "EditCompany.aspx?CID=" + rowView["CID"].ToString();

    ...
}


这篇关于在GridView中创建事件以动态创建超链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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