Solved

Change datanavigateurlformatstring of hyperlinkfield at runtime

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now