I'm using literal to implement css, so as to have the css class vary depending on the page I'm on, the css being applied to some "a" tags also.
That bit (variable css on a tags) seems to be working for me at present for some reason, but the code breaks the use of the ~/ within links...
Does anyone know if there any consequences of using the below but limiting my pages to the root folder - as it does seem to be working there at least:
<li>
<a <asp:Literal id="ClassToUse" runat="server"/> id="home_link"
href= "Default.aspx" title="Home" onfocus="this.blur();" runat="server">Home</a>
</li>
Or if it is possible to get the path from root (~/) working within it, so as to enable a better folder layout/hierarchy.
I don't have an Openid and have given up on trying to sign in - for a while.
A few alternatives come to my mind:
Use a <asp:Hyperlink> and set CssClass property of the hyperlink instead of setting the value of the ClassToUse literal.
Fix the CSS class name of the link but change the CSS definitions by putting the <style> section on your page into a changeable literal.
But it's also possible that I misunderstood your question...
Thanks for the help Heinzi - this one got both working together for me... Went "You beauty" when it worked :)
<li><asp:Hyperlink id="ClassToUse" runat="server" NavigateUrl="~/Default.aspx" title="Home" onfocus="this.blur();">Home</asp:Hyperlink></li>
Case "Default.aspx"
' switch the background pics for the li
ClassToUse.CssClass = "active"
Related
For example, I have two sibling pages Index.aspx and Orders.aspx in one folder. On the page Index.aspx I have the link to Orders.aspx. What is the correct way to implement this:
<a runat="server" href="~/Orders.aspx">
or
<a href="Orders.aspx">
I know what runat="server" does (server-control, performance impact etc.).
You really never need to run markup with a run at server tag if it's never used in code behind, if it is then you should use a ASP.NET control for it.
So just a hyperlink without runat=server would be fine.
It's always best to use ASP.NET controls on your page though if an upgrade in the future could require language translations, or have some logic assigned to them in the future. So always plan ahead on your designs.
If both views are in the same folder, than the second one:
<a href="Orders.aspx">
The deal is that when you have the following on your site it works:
Click Me!
But the above fails to work if that site is a nested application.
I have seen two solutions to this opportunity in asp.net.
The first solution that I found was to add the following:
<a runat="server" href="~/Some/Path/file.aspx" class="button">Click Me!</a>
I have not tried this because I feel it would add more crap to your view state.
The solution that I have tried is:
Click Me!
The question is, what is the preferred method of addressing paths in nested applications?
You want to use HyperLink server control. It is meant for that kind of thing.
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl="~/Some/Path/file.aspx"
CssClass="button">Click Me!</asp:HyperLink>
Render as
<a id="HyperLink1" class="button" href="/Some/Path/file.aspx">Click Me1</a>
ASP.Net HyperLink control uses ResolveClientUrl to resolve the given url, so you do not need to do anything.
I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));
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');
I've got a couple of ASP.Net Usercontrols that I am using in different locations of my new website. These usercontrols had links like this :
If the usercontrol is used in pages in various subdirectories the relative path just doesn't work, and I don't want to provide my full website name in the path. So I did this
<a href="~/daily/panchang/" runat="server">
and now the ASP.Net '~' marker works correctly to resolve the root path.
Is it okay to mark all my HTML tags where I have the need to resolve the root path with runat="server" or do you know of a better, HTML way?
Thanks
I won't say whether it's an elegant solution, I'll just point out an alterantive within System.Web:
<a href="<%= VirtualPathUtility.ToAbsolute("~/daily/panchang/") %>">
You should use a base tag to define the root of your application and make all links relative like this :
<head>
<base href="<%= Request.ApplicationPath %>" />
</head>
...
<!-- this now points to ~/daily/panchang/ -->
Be careful though because every element that has runat="server" will be 'serialized' and stored in the ViewState every time a PostBack occurs, and you don't wanna be cluttering it up with useless data.