Solved

Change datanavigateurlformatstring of hyperlinkfield at runtime

Posted on 2009-05-10
4
1,618 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Shiva-Kumar
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24347905
As per my knowledge, there is no direct possibility to do so. However, there are alternate ways to do so.

Set DataNavigateUrlFormatString protery to http://abcde.com/index.jsp?ID=#ID#&question={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

0
 

Author Comment

by:Shiva-Kumar
ID: 24347960
You'r the man!

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

Thanks
0
 

Author Closing Comment

by:Shiva-Kumar
ID: 31579892
Thanks for your help.
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24347991
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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