issue with asp:hyperlink and NavigateUrl - asp.net

I am having an issue with using the NavigateUrl within a HyperLink asp.net tag.
The navigationUrl works as in go to the page I want it to go, but when I try to get the Query String "n" it gives me <% instead of the number value within the DataBind. Here is the code:
<asp:HyperLink runat="server" NavigateUrl="~/MyWebsite/info.aspx?n='<%#DataBinder.Eval(Conatainer.DataItem, "num")%>' />
Thank you for your help!

Try with:
<asp:HyperLink runat="server" NavigateUrl='<%# String.Format("~/MyWebsite/info.aspx?n={0}", DataBinder.Eval(Container, "DataItem.num").ToString())%>' >Text</asp:HyperLink>

Related

use variable in mailto hyperlink in asp.net

I want to use a variable declared in my .cs page(C#) as cc to mailto, but dont know how to use. Please help.
In default.aspx.cs page:
var code=cc#test.com;
In default.aspx page:
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc=code" ToolTip="Submit" Target="_blank">mail</asp:HyperLink>
This is not a good way to do this but you can set the NavigateUrl from code behind.
Try this:
HyperLink5.NavigateUrl="mailto:to#test.com?subject=Hi&cc="+code;
Have you tried embeding code like
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc="<%=code%> ToolTip="Submit" Target="_blank">mail</asp:HyperLink>

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")%>'

Passing Value Through Link Button in ASP.NET

Conditions :
I have two tables,
category(id_kat,name_kat)
book(id_book,id_kat,name_book)
id_kat has a child and parent relations.
I'm a little bit confused using ASP.NET, especially when passing value between pages.
Now, i'm showing the category's datas with
<asp:listview id="listview1" runat="server" datakeynames"id_kat" datasourceID="sdskat">
//FYI sdskat is datasource for category table
<itemtemplate>
<li>
<asp:linkbutton id="linking" runat="server" ><%# Eval("name_kat") %></asp:linkbutton>
</li>
</itemtemplate>
</asp:listview>
The problem is, i wanted to pass "id_kat" value when i click the hyperlink. (but redirected to self page).
I want it as a query string (or another way if able).
Thanks a lot.
If you're linking to the same page with different query string parameters, you only need to include ? and after.
<%# Eval("name_kat") %>
You could use <asp:hyperlink id="linking" runat="server" ><%# Eval("name_kat") %></asp:hyperlink> instead. You can then add your url in the navigateURL attribute and append your id_kat on there.
Something like this should work:
<asp:hyperlink id="linking" runat="server" navigateURL="~/page.aspx?id_kat=<%# Eval('id_kat') %>" ><%# Eval("name_kat") %></asp:hyperlink>
the <asp:hyperlink> tag is essentially the tag but using a server side control.
If you have to use a linkbutton then in your code behind you can append the id_kat to the querystring and then response.redirect(url) to the new page.
Why not use a regular anchor?
<a href='foo.aspx?id=<%# Eval("id_kat") %>'><%# Eval("name_kat") %></a>

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>

Resources