How to remove portion of url when using NavigateURL - asp.net

I am using NavigateURL to dynamically pull in the url of products on a receipt page.
Here is the exact code:
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>"><%#Eval("Product.Name")%></a>
It is placing "/checkout/~/" in each of the url.
How can I remove or correct this?
Thanks!

The simplest thing would probably be to just call .Replace() and replace the unwanted part with a empty string. But it depends relay. Why is it there to begin with? Where is the data coming from?

I ended up switching from a regular href to an asp:HyperLink and it corrected the /~/ issue.
So, before I was using
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>" runat="server"><%#Eval("Product.Name")%></a><br/>
And I switched it to:
<asp:HyperLink CssClass="blue13" runat="server" NavigateUrl='<%#Eval("Product.NavigateUrl")%>' Text='<%#Eval("Product.Name")%>'></asp:HyperLink>
Which correct the issue.
Thanks.

Related

HttpUtility.UrlEncode() and Server.UrlEncode() are not encoding the URL in asp.net 4.0

I am trying to encode the url using HttpUtility.UrlEncode,
but not able to make it work. HyperLink control is in the
template field of gridview.
Can anyone please help me out with this?
I have tried changing the target application and by adding
System.Web reference to project but still not able to Encode
the url.
Please guide me.
Following is part of my code:-
<asp:HyperLink ID="lnkDetailsPage" runat="server" NavigateUrl='<%# string.Format("~/YellowPages/YellowDetailView.aspx?CustomerId={0}",HttpUtility.UrlEncode(Eval("CustomerId").ToString())) %>'></asp:HyperLink>
Thanks in advance.
Use Server.UrlEncode() instead. Why do you need to encode your CustomerID?!

Image Hyperlink in ASP.NET - MVC 4

I try to create a project for the first time in asp.net (mvc4).
and what i try to do is to create a image which is a hyperlink to go to the index page.
i have search a lot of things and it shows very simple to do that.
but i can´t understand why it doesn´t work for me.
someone can give a hand?
Code:
<a href="<%= Url.Action("Index","Home")%><img src="~/Content/imagens/nav-arrow-back.png"/></a>
The Action is "Index" in the controller calls Home.
you miss a quote
<a href="<%=Url.Action("Index","Home")%>"> ...
^
about this quote you missed
For bad request, fix the whole <img> part
<img src="<%=Url.Content("~/Content/imagens/nav-arrow-back.png")%>"/>
First up, as previously noted you're missing a closing quote on that href. Second, MVC 4 doesn't use the <% %> syntax, at least not by default; it should be using Razor v2 which uses #, so your code should look like this:
<img src="~/Content/imagens/nav-arrow-back.png"/>
If you use the old syntax I assume it would try to handle the actual text <%= Url.Action("Index","Home")%> as a URL, which clearly won't work.

Escape double quotes in asp.net Onclick string

I am trying to insert dynamic data into a onclick property for a control the code looks like this
onclick="openRadWindow('<%#Eval("ID";) %>','<%#Eval("ParentObjectID") %>');"
I cant get it to fire and the trouble seems to be the double quotes, what is the correct method on escaping the quotes so that this fires.
You can do use a format string, like this:
onclick='<%# string.Format("openRadWindow(\"{0}, {1}\");", Eval("ID"), Eval("ParentObjectID")) %>'
Is the event just not firing or are you getting any javascript errors as well. Also, I would look at the HTML after the page has been rendered and make sure that the server tags are being processed correctly. There are certain uses that cause them not to actually be processed and will remain <%# Eval("ID") %>.
Thanks to all I was able to get it working correctly using a different method. in the code behind I created a function and in the function I put the following code
Return String.Format("openRadWindow({0},{1});", photo.ID, photo.ParentObjectID)
and in the aspx I added onclick="<%#MyFunction(DirectCast(Container.DataItem,Photo))%>
First thing I see is that semi-colon after "ID" - I think that might be causing your problems.

How to bind Request.QueryString[""] to Eval() in ASP.NET

ImageUrl='<%#Eval("Name","../Master Pages/DisasterImages/") %>'+'<%#Eval("Request.QueryString["DisID"].ToString()/{0}") %>'
DisID is a folder name.
What I want is to display the images in the directory.
I have done it using a repeater control. The problem is now I want to get the folder name in the QueryString. How can I do this? Above is the code I have tried without achieving the desired outcome...
Any help would appreciated.
Thank you!
use <%= instead of <%#. Example:
<%= Request.QueryString["DisID"] %>
I have noticed another issue with your code, you need to be careful when using quotes inside a string. You need to escape them. Therefore
"Request.QueryString["DisID"].ToString()/{0}"
should look like this
"Request.QueryString[\"DisID\"].ToString()/{0}"
Notice the backslash that serves for escaping characters.

Why does ASP.Net rewrite relative paths for runat=server anchor controls?

I have my UserControls in a ~/Controls folder in my solution:
/Controls/TheControl.ascx
If specify the following:
<a id="theId" runat="server" href="./?pg=1">link text</a>
ASP.Net seems to want to rewrite the path to point to the absolute location. For example, If the control is on site.com/products/fish/cans.aspx the link href will be rewritten to read
<a id="munged_theId" href="../../Controls/?pg=1>link text</a>
Why does Asp.Net rewrite these control paths, and is there an elegant way to fix it?
I just want the anchor control to spit out exactly what I tell it to!!! Is that so hard?
EDIT:
I've basically done what Kelsey suggested. I knew I could do it this way, but I don't like adding markup in my code when I want something relatively simple. At least it solves the problem:
Aspx page:
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
Code-behind:
var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);
As you can see by the amount of code needed for what is essentially supposed to be be a simple and light-weight anchor, it's not the most optimal solution. I know I could use a Literal but I figured this was cleaner as I'm adding more than one anchor.
I would be interesting in knowing WHY ASP.Net takes over and tries to fix my URL, though.
Why do you have runat="server" and no ID defined? Do you need to access it server side? If you remove the runat="server" everything will work as expected.
For more information regardinging how ASP.NET handles paths check out this MSDN article.
Edit: You can get around the problem then by using a Literal control and then outputing the raw <a href... to it.
Eg:
<asp:Literal ID="myLiteral" runat="server" />
myLiteral.Text = "link text";
Then you can set the visible property on the Literal however you want.
I know this is a bit of an old topic, but I was running into this problem as well and in the end went with a similar solution, but was able to save a few lines of code by doing this in the ascx:
<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>
Then in the code behind, I referenced it using an HtmlGenericControl and can then do this:
myAnchor.TagName = "a";
// other properties set as needed
Anyway, I thought I'd post in case anyone else stumbles in here with the same issue.
Best bet is to make everything app root relative using the magic ~/ lead-in to the url. That tends to keep stuff straight.
There isn't a great answer to your question. ASP.NET is going to treat a relative path in a UserControl as relative to the path of the user control.
What you can do is in the code behind for your user control, set the HRef property of your anchor tag based on the Request.Path property. Then you can create URLs relative to the page.
Alternative is to use a literal like Kelsey was suggestion, or I would just try and map everything app relative with ~/ like Wyatt suggested.
Even a literal doesn't work using ICallBackEventHandler and RenderControl at least... I ended up hacking the tag back client-side :/ e.g in JQuery:
$('#munged_theId').attr('href', './?pg=1');

Resources