Change datanavigateurlformatstring of hyperlinkfield at runtime


How do i change the DataNavigateUrlFormatString of the given code below to "{0} at run time.

the ID is not coming from database, its based on the query string,
ex  if lang = EN then ID = 1
     if lang = ES then ID  = 2

I have hidden field in my code which holds the ID based on the query string and i tried adding the refrence to the the datanavigateurlformatstring with no luck

DataNavigateUrlFormatString = "'<%=ID.ClientID%>'&question={0}"

Please suggest any alternate suggestion.

<asp:GridView ID="Answer" runat="server" ShowHeader="False" ">
                        <asp:HyperLinkfield DataNavigateUrlFormatString = "{0}"
                        DataTextField="IRAnswer"   DataNavigateUrlFields = "Answer"  Target = "_blank" Text = "Click Here" />

Open in new window

Who is Participating?
Muhammad Ousama GhazaliConnect With a Mentor Solution Analyst & ArchitectCommented:
As per my knowledge, there is no direct possibility to do so. However, there are alternate ways to do so.

Set DataNavigateUrlFormatString protery to{0} and use the code snippet provided in the RowDataBound event of the GridView to achieve what you require. Make sure to change the index 0 of Cells(0) part in the code to match with your GridView columns.

Hope this helps.

If e.Row.RowType = DataControlRowType.DataRow Then
	Dim lnkTemp As HyperLink = CType(e.Row.Cells(0).Controls(0), HyperLink)
	lnkTemp.NavigateUrl = lnkTemp.NavigateUrl.Replace("#ID#", HiddenFieldName.Value)
End If

Open in new window

Shiva-KumarAuthor Commented:
You'r the man!

That was perfect.  Thanks buddy for all your help.  It worked like a charm

Shiva-KumarAuthor Commented:
Thanks for your help.

The HyperLinkField does not have the ID property, hence You cannot directly reference it from code behind. However, simply convert the HyperLinkField to a template field and You can reference it from code behind.

        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Answer", "{0}")%>' Text="Answer">
Code behind:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ID As Integer = 0
            If Not String.IsNullOrEmpty(Request("lang")) Then
                If Request("lang") = "EN" Then
                    ID = 1
                ElseIf Request("lang") = "ES" Then
                    ID = 2
                End If
            End If
            Dim hl As HyperLink = CType(e.Row.FindControl("HyperLink1"), HyperLink)
            hl.NavigateUrl = hl.NavigateUrl.ToString + "&ID=" & ID
        End If
    End Sub

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.