Dynamically setting Hyperlinks in GridView from code behind

I was looking for a way to set the url of a HyperlinkField from the code behind but I couldn’t find what I needed so I was able to find a solution by modifying code that I use to add required field validators to a column.

Instead of using an TemplateField or HyperlinkField I am just using a standard BoundField then adding a hyperlink to it as part of the RowDataBound event.

HTML

<asp:BoundField DataField="ID" HeaderText="ID" />

Code behind RowDataBound event

Dim HyperLink As New HyperLink With {
                .Text = e.Row.Cells(0).Text.ToString(),
                .NavigateUrl = "view_item.aspx?id=" + e.Row.Cells(0).Text.ToString()
                }
            e.Row.Cells(0).Controls.Add(HyperLink)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Answer is in the question, hope this helps somebody.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x