• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1839
  • Last Modified:

Create Dynamic Hyperlink in DataGrid Column

I have a project that i am working on.
And this project has a DataGrid in the ASP.net file

Here is what i want to accomplish
i want to have the last column of my datagrid be a dynamic link.
ITEM ID             Descrip                    item_details
2161                 some item                 Details <------------i want this to be a link that looks like this http://somesite.com/itemdetails.aspx?item_id=2161

there is a list of about 21000 items so dynamicly is the the only way.
So how should i do this in Visual Studio?
1 Solution
Try this....
Firstly you'll need to take a template column.
In the template column, take a hidden control and a literal control.

Bind the hidden control value to something like
<asp:HiddenField id="hdnId" runat="server" value='<% #DataBinder.Eval(Container.DataItem,"ItemID") %>' />
And the literal control should look something like this.

<asp:Literal id="litLink" runat="server" />
Now in the ItemDataBound event of the gridview write this

I think this should work...

Literal litLink=(Literal)e.Row.FindControl("litLink");
HiddenField hdnID = (HiddenField)e.Row.FindControl("hdnId");
litLink.Text = "<a href='http://somesite.com/itemdetails.aspx?item_id='"+hdnID.value+">"

Open in new window

phoenixfire425Author Commented:
i tried what you said and it did not work.
however i did get this to work.

<asp:HyperLinkField DataNavigateUrlFields="order_no" DataNavigateUrlFormatString="OrderDetail.aspx?order_number={0}"
                    DataTextField="order_no" HeaderText="Order_details" SortExpression="order_no"
                    Target="_blank" />

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now