简体   繁体   English

如何在asp.net中动态添加Hyperlink到GridView Row

[英]How to add Hyperlink dynamically to GridView Row in asp.net

I have a GirdView populated with text from code behind, I want to have a row contains a text and another row contains a HyperLink . 我有一个GirdView填充了代码后面的文本,我希望有一行包含一个文本而另一行包含一个HyperLink

I use the following code : 我使用以下代码:

  Dim dtFull As New DataTable()
            ' Create four typed columns in the DataTable.
        dtFull.Columns.Add("Self Assessment", GetType(Object))
        dtFull.Columns.Add("Staff Development", GetType(Object))
        dtFull.Columns.Add("Mid Meeting", GetType(Object))
        dtFull.Columns.Add("Objectives Rating", GetType(Object))
        dtFull.Columns.Add("Overall Rating", GetType(Object))
        dtFull.Columns.Add("Second Supervisor Approval", GetType(Object))
        dtFull.Columns.Add("Staff Sign Off", GetType(Object))
        dtFull.Columns.Add("Total", GetType(Object))

        dtFull.Rows.Add("", "", "", "", "", "", "", "")
        gvGeneralStatistics.DataSource = dtFull
        gvGeneralStatistics.DataBind()

How to add HyperLink to the GridView row : 如何将HyperLink添加到GridView行:

try to have a TemplateField in your GridView. 尝试在GridView中使用TemplateField。 and add the hyperlink inside the template field. 并在模板字段中添加超链接。 you set the hyperlinks values manually from the GridView Columns, or from the code behind in even Row Data Bound! 您可以从GridView列手动设置超链接值,也可以从甚至Row Data Bound中的代码中手动设置超链接值!

I prefer to use TemplateField coz of its simplicity. 我更喜欢使用简单的TemplateField。 But if you want to add dynamically you can do it like 但是,如果你想动态添加,你可以这样做

C#: C#:

HyperLinkField hplnk = new HyperLinkField();
hplnk.DataTextField = "YourValue";
hplnk.HeaderText = "yourHeaderTextValue";
gvGeneralStatistics.Columns.Add(hplnk);

Vb.net: Vb.net:

Dim hplnk As New HyperLinkField()
hplnk.DataTextField = "YourValue"
hplnk.HeaderText = "yourHeaderTextValue"
gvGeneralStatistics.Columns.Add(hplnk);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM