Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Change datanavigateurlformatstring of hyperlinkfield at runtime

Posted on 2009-05-10
4
Medium Priority
?
1,650 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 2000 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

Independent Software Vendors: 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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

963 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