How to access a HyperlinkField from Code behind?

Posted on 2009-04-23
Last Modified: 2013-11-08
Here is some code snippets that show how I am trying to access a HyperLinkField in a GridView from the code page. I seem to be doing this wrong because the line in code behind where I try to get a handle on the control fails because the Controls collection is empty.

How should I be doing this?  My goal is to DataBind a DataSet to the HyperLinkFields I have built (works) and then slip in some URLs into the HyperLinkFields to go with them (no work).

Thank you for your time,

<asp:GridView ID="GridView1" runat="server" CellPadding="4" AutoGenerateColumns=false




                <asp:HyperLinkField DataTextField="Column1" />                



protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)


        HyperLink link = (HyperLink)e.Row.Cells[0].Controls[0];  //this is where I get an index out of bounds because teh collection is empty

        link.Text = "test";


Open in new window

Question by:davecove
    LVL 15

    Accepted Solution

    How about using a TemplateField instead, inside of it place a hyperlink tag which uses DataBindings to set it's href & text values.

    I've bounds to the "ID" field, but you'd use whatever's in your dataset
    <asp:TemplateField HeaderText="Link">
        <asp:HyperLink runat="server" NavigateUrl='<%# "/foo.aspx?" + Eval("ID") %>' Text='<%#Eval("ID") %>' ></asp:HyperLink>

    Open in new window


    Author Comment

    That seems reasonable... but with the code below I still get an Index out of range error when I try to pick the control out of the cell in the RowDataBound handler.

    What am I missing?

    //moving to something slightly simpler
            <asp:Label runat=server ID="Label1" Text='<%#Eval("Column1")%>'></asp:Label>
    code behind:
    Label link = (Label)e.Row.Cells[0].Controls[0]; //index error here, the Controls collection is empty
    link.Text = "test";

    Open in new window

    LVL 15

    Expert Comment

    If the e.Row.Cells[0] is a Header cell, it may very well not contain any child controls. Also what's the point of manually setting the link's Text property? All you're doing is overwriting the bound value?

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now