在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();

    ...
}