Learn how to a build a cloud-first strategyRegister Now


How to access a HyperlinkField from Code behind?

Posted on 2009-04-23
Medium Priority
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
  • 2
LVL 15

Accepted Solution

oobayly earned 1000 total points
ID: 24219619
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

ID: 24225854
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

ID: 24226090
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

810 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