.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET GridView: How To Add A HyperLinkColumn Dynamically


I have been searching all over the web for a way to do this, and it doesn’t appear as if anyone else knows! How do you add a HyperLinkColumn to a GridView dynamically at runtime? If this isn’t possible, is there a way to modify its properties? I can’t seem to access the HyperLinkColumn to modify the Target property, which doesn’t seem right.  Shouldn’t you be able to access any property programmatically?

Status: Solved! (10/23/07)
Solution: See comment by Kevin Brock. Thanks, Kevin!

September 24, 2007 - Posted by | ASP.NET, GridView |

13 Comments »

  1. I had to do this to change the NavigateUrl property, so should be similar to change the Target property as shown here. This changes the property when new data is bound to the grid view row. It dynamically searches for the hyperlink column so that column may be moved in the designer. If you have more than one hyperlink you will need some other way to identify the one(s) you need to change.

    (Note this accomplishes the second option. The hyperlink object is defined in the dataview designer but I modify one of its properties)

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType != DataControlRowType.DataRow)
    return;

    // Locate column with hyperlink object
    TableCellCollection cells = e.Row.Cells;
    HyperLink hl = null;
    foreach (TableCell c in e.Row.Cells) {
    if (c.Controls.Count > 0)
    {
    hl = c.Controls[0] as HyperLink;
    if (hl != null)
    break;
    }
    }
    if (hl == null)
    return;

    hl.Target = “different.target”;
    }

    Comment by Kevin Brock | October 23, 2007 | Reply

  2. Thanks, Kevin! Here’s the translation to VB.NET for those who need it:

    Protected Sub gridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType <> DataControlRowType.DataRow Then
    Return
    End If

    ‘ Locate column with hyperlink object
    Dim cells As TableCellCollection = e.Row.Cells
    Dim hl As HyperLink = Nothing
    Dim c As TableCell

    For Each c In e.Row.Cells
    If c.Controls.Count > 0 Then
    hl = c.Controls(0) as HyperLink
    If Not hl Is Nothing Then
    Exit For
    End If
    End If
    Next

    If hl Is Nothing Then
    Return
    End If

    hl.Target = “different.target”
    End Sub

    Comment by Some.Net(Guy) | October 23, 2007 | Reply

  3. Sir,
    This is a good example. Could you please help me out for the following one?

    I have gridview which contains some user related info. There is a column in gridview which contais UserId. A user can has multiple IDs. In dotnet you can hyperlink cell values in gridview. Now what I need. Since user can have more than a ID each ID will nevigate a seperate page. For an example;

    UserID – Sajid01 – NevigateUrl (test.aspx)
    UserID – Sajid02 – NevigateUrl (test001.aspx)

    So when the gridview populating user can click on the userid and it will redirect to it’s corresponding page.

    This hyperlinks are conditional. I mean:

    IF (UserModule=1) THEN NevigateUrl(test01.aspx)
    IF (UserModule=2) THEN NevigateUrl(test02.aspx)
    IF (UserModule=3) THEN NevigateUrl(test03.aspx)

    How is it possible?

    Please help me out to somve this problem.

    Thanks

    Sajid

    Comment by Sajid Wasim | January 1, 2008 | Reply

  4. i have a problem, i need to add hyperlink.
    to my myCells[2]

    TableCellCollection myCells = e.Row.Cells;
    myCells[0].Width = Unit.Pixel(44);
    myCells[0].CssClass = “txt_time”;
    myCells[1].Width = Unit.Pixel(550);
    myCells[1].CssClass = “txt_descrip”;
    myCells[2].Width = Unit.Pixel(43);
    myCells[2].CssClass = “link_rating”;
    myCells[2].ToolTip = “Age Restiction”;

    Comment by nick | February 11, 2008 | Reply

  5. Nick –

    Instead of making that a regular column, make it a HyperLinkColumn. I don’t believe you need to dynamically insert it.

    Comment by Some.Net(Guy) | February 11, 2008 | Reply

  6. If you don’t want to go through the hassle of looping through your recordset, you could just add a template field that allows you to specify the elements of a URL.

    For my C# app I have this:

    <a href=”” target=””>

    Comment by Kathryn | April 3, 2008 | Reply

  7. Apologies if this doesn’t work either, but I’m sure you can work it out for yourselves…

    <asp:TemplateField>
    <ItemTemplate><a href=”<%# Eval(“Link”) %>” target=”<%# Eval(“Target”) %>”><%# Eval(“LinkText”) %></a>
    </ItemTemplate>
    <EditItemTemplate><asp:TextBox ID=”txtLinkText” runat=”server” Text=”<%# Eval(“LinkText”) %>”></asp:TextBox></EditItemTemplate>
    </asp:TemplateField>

    Comment by Kathryn | April 3, 2008 | Reply

  8. Hi

    Can someone please help me. I need my Users to Upload a picture to the Database and then that picture must be displayed in ‘n DataGrid with the User’s data.

    How can i do this?

    Kind Regards
    Etienne

    Comment by Etienne | July 23, 2008 | Reply

  9. Thanks for the information

    Comment by Stephen | August 3, 2008 | Reply

  10. I use a .NET search engine designed to find .NET answers. Might help you.

    http://www.helpwithdotnet.com/

    Rachel

    Comment by Rachel | August 4, 2008 | Reply

  11. we have one web form and on that 1 gridview .in this gridview we boumd name column this names are hyperlink when user click on that name ,this wants to display on another page in label. pls help

    Comment by pallavi deshmane | April 20, 2009 | Reply

  12. pls can u send asp.net coding on my emailid

    Comment by pallavi deshmane | April 20, 2009 | Reply

  13. Hi you can actually create the Hyper link column dynamically using templates implementing ITemplate
    for reference
    Adding HyperLink Column to GridView dynamically

    Comment by Vb Reader | September 15, 2009 | Reply


Leave a comment