Solved

Change datanavigateurlformatstring of hyperlinkfield at runtime

Posted on 2009-05-10
4
1,614 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

840 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