Link to home
Start Free TrialLog in
Avatar of Shiva-Kumar
Shiva-Kumar

asked on

Change datanavigateurlformatstring of hyperlinkfield at runtime

hi,

How do i change the DataNavigateUrlFormatString of the given code below to "http://abcde.com/index.jsp?ID=2&question={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 = "http://abcde.com/index.jsp?ID='<%=ID.ClientID%>'&question={0}"

Please suggest any alternate suggestion.


Thanks
<asp:GridView ID="Answer" runat="server" ShowHeader="False" ">
                    <Columns>
                        <asp:HyperLinkfield DataNavigateUrlFormatString = "http://abcde.com/index.jsp?ID=1&question={0}"
                        DataTextField="IRAnswer"   DataNavigateUrlFields = "Answer"  Target = "_blank" Text = "Click Here" />
                        </Columns>
</asp:Gridview>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Muhammad Ousama Ghazali
Muhammad Ousama Ghazali
Flag of Saudi Arabia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shiva-Kumar
Shiva-Kumar

ASKER

You'r the man!

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

Thanks
Thanks for your help.
Hi,

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.


Markup:
 
<asp:TemplateField>
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Answer", "http://abcde.com/index.jsp?question={0}")%>' Text="Answer">
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>
 
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