ASP.NET multiple Eval fields - asp.net

I need to include multiple Eval fields in the Navigate URL field of a hyperlink control, however, the code I have tried does not work.
<asp:HyperLink ID="hlkImageLink" runat="server" NavigateUrl='<%# Eval("getProductIDGV","getProductCategoryNameGV","getProductCategoryIDGV", "~/PT_productdetails.aspx?ProductID={0}&CategoryName={1}&CategoryID={2}") %>'>

Try:
NavigateUrl= '<%# String.Format("~/PT_productdetails.aspx?ProductID={0}&CategoryName={1}&CategoryID={2}", HttpUtility.UrlEncode(Eval("getProductIDGV")), HttpUtility.UrlEncode(Eval("getProductCategoryNameGV")), HttpUtility.UrlEncode(Eval("getProductCategoryIDGV"))) %>'

Related

Gridview Hyperlinkfield using DataNavigateUrlFormatString with Ampersand

So I have gridview pulling fields from a table and my hyperlinkfield is used to go to a specific page for that row to get more detailed data. Everything seems to work great except when the field used in the hyperlinkfield has an ampersand. I assume it is reading the ampersand as something else and so it doesn't bring up the proper info because the ampersand is in the name in the database.
Hyperlinkfieldcode:
<asp:HyperLinkField HeaderText="Name" Text="{0}" DataNavigateUrlFields="Name" DataNavigateUrlFormatString="item.aspx?name={0}" DataTextField="Name" />
Example:
A clicking on the name "test item" would take you to mysite.com/item.aspx?name=test%20item and this works.
However, clicking on "test & test item" it takes you to mysite.com/item.aspx?name=test%20item%20&%20item which does not work. It just pulls up a page with blank info.
What can I do to fix this?
Update:
I converted the hyperlinkfield to a hyperlink inside a template field, but the url is now coming out weird.the url now comes out like mysite.com/item.aspx?name=System.Int32%5b%5d
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Name") %>' DataNavigateUrlFields="Name" NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode({0}.ToString())%>' DataTextField="Name" />
</ItemTemplate>
</asp:TemplateField>
My original answer was incomplete (due to not being able to see the entire code). This answer will contain the missing elements.
The main object is to UrlEncode the data field Name, so that it can be used as (part of) a url link .
1 - First we must ensure that the field "Name", is listed as DataKeyNames for the GridView as follows:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name" ...
2 - Secondly (if using a navigateURL) create a TemplateField i.e. (asp:TemplateField ) and use Eval() method to access the DataField in conjunction with UrlEncode() .
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="NameLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode(Eval("Name").ToString())%>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Option 2 Alternatively you can use a HyperLinkField and DataNavigateURL but you should still designate Name as a DataKeyNames .
Hope this works with no problems. Let me know if any clarification is needed.
Happy coding and Cheers,

How to handle single quote in query string parameter values inside Eval for navigateurl property of hyperlink

Inside Gridview control
If my ID_Logon value has single quote characters in it, then the string gets terminated at the single quote.
for eg, if Id_Logon = O'connel
then only the O is being passed as a parameter. How to pass the whole string?
<asp:TemplateField HeaderText="LogonID" >
<ItemTemplate>
<asp:HyperLink ID="hyperlink1"
NavigateUrl='<%#"EditLogon.aspx?ID=" + Eval("ID_Logon")%>'
Text='<%# Bind("ID_Logon")%>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Try this
NavigateUrl='http://home/?<%# Eval("U_ID") %>'
or
NavigateUrl='<%# "http://home/?" + (string)Eval("U_ID") %>'
source asp:hyperLink NavigateURL and Eval functions

asp.net gridview edit template http link

I have a label within an Edit Template for a gridview which goes like this:
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Project_ID") %>'></asp:Label>
I would like to turn that label text into a http link like so:
<asp:Label ID="lblProjectID" runat="server" Text='<a href=http://intranet/?<%# Bind("Project_ID") %>> <%# Bind("Project_ID") %></a>'></asp:Label>
So the link would look something like http://intranet/?Project_ID
But that syntax is incorrect. What is the correct way to write that?
This is what you can do on your Label tag.
<asp:TemplateField>
<ItemTemplate>
<a href='<%# String.Format("http://intranet/?Project_ID={0}", Eval("Project_ID")) %>'><%# Eval("Project_ID")%></a>
</ItemTemplate>
</asp:TemplateField>
If you want it to be a link... then just use a link, not a label:
<a href='http://intranet/?<%# Eval("Project_ID") %>'><%# Eval("Project_ID")%></a>
or same thing with HyperLinkField (if you want to use it as a column and not inside EditItemTemplate:
<asp:HyperLinkField DataTextField="Project_ID" DataNavigateUrlFields="Project_ID" DataNavigateUrlFormatString="http://intranet/?{0}" />

hyperlink in a item template inside the gridview

I have this hyperlink in a item template inside the gridview
<asp:TemplateField Headertext ="SN0">
<ItemTemplate>
<asp:Hyperlink runat= "server" Text='<%# DataBinder.Eval(Container.DataItem,"Container.DataItemIndex + 1")%>'
NavigateUrl='<%# "ResolveComplaint.aspx?Name=" & DataBinder.Eval (Container.DataItem,"ComplaintProfileId").tostring & _
"&Status=" & DataBinder.Eval(Container.DataItem,"Status").tostusring %>' ID="Hyperlink2"/>
</ItemTemplate>
</asp:TemplateField>
Basically, I am trying to make the first column(SN0) in a gridview. On click on the hyperlink, it redirects to another Page. I am carrying ComplaintProfileId, Status fields
to the next page
This gives me a compiletime error:
Compiler Error Message: CS1026: ) expected
Thanks
Sun
The problem is when you are trying to set the NavigateUrl property. You are using the & for concatenation, but you have to use the + sign for that. e.g.
NavigateUrl='<%# "ResolveComplaint.aspx?Name=" + DataBinder.Eval (Container.DataItem,"ComplaintProfileId").tostring +
"&Status=" + DataBinder.Eval(Container.DataItem,"Status").tostusring %>'
Should your .tostring and tostusring calls be .ToString()?

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