Modifying querystring on asp:hyperlink? - asp.net

My code looks like this:
<asp:HyperLink ID="HyperLink1" NavigateUrl="Search.aspx?txtGraphicNumber=" target="_blank" runat="server">Search Related</asp:HyperLink>
My querystring starts with "txtGraphiNumber=". What I'd like to do is append to this querystring the information in a field on the screen. The field is called txtGraphicNumber.
Can I do something like:
<asp:HyperLink ID="HyperLink1" NavigateUrl="Search.aspx?txtGraphicNumber=" + txtGraphicNumber + " " target="_blank" runat="server">Search Related</asp:HyperLink>

You have two options to achieve the same:
Option 1# Code behind:
//assuming txtGraphicNumber is a textbox.
HyperLink1.NavigateUrl += txtGraphicNumber.text;
Option 2# InlineCode:
<asp:HyperLink ID="HyperLink1" NavigateUrl='<%="Search.aspx?txtGraphicNumber=" +
txtGraphicNumber.value %>' target="_blank" runat="server">Search Related
</asp:HyperLink>

Just do it from code-behind:
HyperLink1.NavigateUrl += txtGraphicNumber;
If txtGraphicNumber is a literal string value. You could use inline ASP.NET script for constructing primitive HTML controls and their values/attributes but this doesn't play nice with managed server-side controls.

Related

How to reference a .Net web control's selected value in NavigateURL

I have an ASP.Net page with a GridView. In one of the GridView cells there's a HyperLink control and its NavigateURL property is set like so:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") %>'
There's a RadioButtonList (rblDeviceType) on this page (not in the GridView) with four values. I want to add another querystring to the HyperLink's NavigateURL so that:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") + "&devicetype=" + rblDeviceType.SelectedValue %>'
This is of course not correct syntax. Is there a way to do this?
Try this:
In your html
<a href='<%= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=", this.someValue, rblDeviceType.SelectedValue) %>'>
Hello World
</a>
or in your html:
<asp:HyperLink runat="server"
NavigateUrl='' ID="demoLink">
Hello World
</asp:HyperLink>
and then in your codebehind:
demoLink.NavigateUrl= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=",this.someValue,rblDeviceType.SelectedValue)
Regarding
'someValue'
Which you present as Eval("IMEI") in your sample code since your code is not part of the Grid you will need to get this from either a control directly, session, viewstate or server side variable. Your code sample does not allow me to understand where is the original source of this value.
Try this in your code behind:
public partial class _Default : Page
{
public string someValue = "Hello World";
Using string.Format and <%= instead of <%#

asp:HyperLink run-time modification

<asp:HyperLink ID="HyperLink5" runat="server" CssClass="RegAgenda"
NavigateUrl='<%#"http://www.mysite.com/store/" &
DataBinder.Eval(Container.DataItem,"DESCRIPTION")& "?ALID=" &
DataBinder.Eval(Container.DataItem,"TransID")%>'
Target="_blank" Text="Info/Buy Now">
</asp:HyperLink>
In the above asp:HyperLink code, I would like to test if the current page has Chuy2 in the path and if so, change the base url to http://www.mysite2.com
how would I do that? I am a PHP guy and don't know ASP.Net.
Something like this should work:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# string.Format("{0}/{1}?alid={2}", Request.Url.AbsoluteUri.Contains("Chuy2") ? "http://www.mysite2.com" : "http://www.mysite.com/store", Eval("Description"), Eval("TransID")) %>'
rather than try to piece this together in the markup/template, I would build the url in the code behind, or view model and then bind that value. the markup might look like this
NavigateUrl='<%#Eval("Url")%>'

Using fixed text together with <%# when databinding in Asp.NET doesn't work

This works:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/MyCustomUrl.aspx
This doesn't:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="subfolder/<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/subfolder/<%# GetCustomUrl() %>
I'm doing this in a Column/TemplateField of an Asp.NET GridView.
Am I doing something wrong or is this simply impossible.
Of course I'm using a workaround now where I set the rest of the NavigateUrl value as well, but I was wondering why this isn't working...
You can do this instead: NavigateUrl='<%# string.Format("subfolder/{0}", GetCustomUrl()) %>'.
Databinding expressions for properties of server controls must be the only thing in the property (no mixing with static text in the way you tried).
You should put the whole expression inside the databinding markup:
<asp:HyperLink ID="EditGridItemLink" runat="server"
NavigateUrl="<%# "subfolder/" + GetCustomUrl() %>">
link
</asp:HyperLink>

How to create RouteUrls with databound parameters declaratively?

I'm using the new Routing feature in ASP.NET 4 (Web forms, not MVC). Now I have an asp:ListView which is bound to a datasource. One of the properties is a ClientID which I want to use to link from the ListView items to another page. In global.asax I have defined a route:
System.Web.Routing.RouteTable.Routes.MapPageRoute("ClientRoute",
"MyClientPage/{ClientID}", "~/Client.aspx");
so that for instance http://server/MyClientPage/2 is a valid URL if ClientID=2 exists.
In the ListView items I have an asp:HyperLink so that I can create the link:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# "~/MyClientPage/"+Eval("ClientID") %>' >
Go to Client details
</asp:HyperLink>
Although this works I would prefer to use the RouteName instead of the hardcoded route by using a RouteUrl expression. For instance with a constant ClientID=2 I could write:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl="<%$ RouteUrl:ClientID=2,RouteName=ClientRoute %>" >
Go to Client details
</asp:HyperLink>
Now I am wondering if I can combine the route expression syntax and the databinding syntax. Basically I like to replace the constant 2 above by <%# Eval("ClientID") %>. But doing this in a naive way...
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%$ RouteUrl:ClientID=<%# Eval("ClientID") %>,RouteName=ClientRoute %>' >
Go to Client details
</asp:HyperLink>
... does not work: <%# Eval("ClientID") %> is not evaluated but considered as a string. Playing around with several flavors of quotation marks also didn't help so far (Parser errors in most cases).
Question: Is it possible at all what I am trying to achieve here? And if yes, what's the correct way?
Thank you in advance!
Use System.Web.UI.Control.GetRouteUrl:
VB:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# GetRouteUrl("ClientRoute", New With {.ClientID = Eval("ClientID")}) %>' >
Go to Client details
</asp:HyperLink>
C#:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>' >
Go to Client details
</asp:HyperLink>
I know it's basically the same as Samu Lan's solution but instead of using .net controls you could use regular HTML anchor control.
<a href='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>'>
Go to Client details
</a>

ASP.NET Querystrings

I got this code in order to build an url for the link using a querystring from the current page. The problem is.... It doens't work. Any suggestions?
<asp:hyperlink ID="link1" runat="server" NavigateUrl='<%#("Equipamentos.aspx?ID_Cliente=")+Request.QueryString ("ID_Cliente").trim.tostring()%>'>Equipamentos</asp:HyperLink>
Gah, my eyes! Try doing this in code behind instead:
link1.NavigateUrl = "Equipamentos.aspx?ID_Cliente=" & Request.QueryString("ID_Cliente").Trim().ToString()
You have to use "&" instead of "+" because this is VB.NET, not C#.
Your ASP.NET code should look like this:
<asp:HyperLink ID="link1" runat="server" NavigateUrl=''>Equipamentos</asp:HyperLink>
And then add this in code behind:
this.link1.NavigateUrl = string.Format("Equipamentos.aspx?ID_Cliente={0}", Request.QueryString["ID_Cliente"].Trim());
Try this instead :
<asp:hyperlink ID="link1" runat="server"
NavigateUrl='<%= ("Equipamentos.aspx?ID_Cliente=")
+ Request.QueryString("ID_Cliente").Trim().ToString() %>'>
Equipamentos</asp:HyperLink>
The
<%# %>
tags are for directives, such as registering controls. You need a
<%= %>
tag, which is called a code evaluation block.
Something like
<%= (5+5).ToString() %>
is what you need - try your code in there.
You are not going to be able to set the NavigateUrl of the link in this way. Try something like this:
<asp:hyperlink
ID="link1"
runat="server">Equipamentos</asp:HyperLink>
And then in your codebehing or a script tag do this:
link1.NavigateUrl = "Equipamentos.aspx?ID_Cliente="
+ Request.QueryString("ID_Cliente").Trim().ToString();
As I know you can't use "<%= %>" with server controls. So you can:
1. Leave it as a server control and follow Andrew Hare's (or similar) answer.
2. Use client control: "<a />" and "<%= %>" should work.

Resources