tags:

views:

555

answers:

3

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 :

<a href="daily/panchang/"></a>

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

+4  A: 

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.

Andreas Grech
What if I turn viewstate off? I don't need in most of the pages.
Cyril Gupta
But then again... I won't know whether I need it till users have a problem.
Cyril Gupta
You can (and should) turn off viewstate in individual controls that you do not need to track viewstate on.
Per Hornshøj-Schierbeck
+5  A: 

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>
...
<a href="daily/panchang/"></a> <!-- this now points to ~/daily/panchang/ -->
Diadistis
But be aware that now ALL your links are relative to the base path...
splattne
:) There's a caveat to everything. Though I like this solution
Cyril Gupta
Yes that's true, but in any good design that should be the case :)
Diadistis
I also think, it's a good solution. I just wanted to you check if there are some common header/footer controls/classes which are "not ready" for that solution.
splattne
It's probably the best you can do :)
Per Hornshøj-Schierbeck
yech, base tags are such a code smell, this is unlikely to be pretty
annakata
Well, it failed miserably when I tried to implement it. My ASP.Net Ajax won't work and the error I got is totally mysterious!
Cyril Gupta
+6  A: 

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/") %>">
Cristian Libardo
That doesn't look as if it does anything different than what I wrote, and looks harder to read. Is it more efficient?
Cyril Gupta
It's actually rather different. The one with runat server will create a HtmlGenericControl and add it to the control hierarchy. This will create a LiteralControl with the computed string.
Cristian Libardo
I wouldn't worry about efficiency. This might be more efficient but it's probably irrelevant. Just know the alternative and write as clean code as possible.
Cristian Libardo
I agree with Cristian - this is much better than your own initial solution - you also avoid the whole viewstate problem.
Per Hornshøj-Schierbeck
I added the EnableViewState="False" to my initial solution and got what's needed. I think it's cleaner to read too. What say?
Cyril Gupta
I say go for it. I don't think the enableviewstate will do any difference unless you set the href in page load. ASP.NET doesn't usually store viewstate for attributes set before "begin tracking viewstate" which happens sometime before load.
Cristian Libardo
Is mixing code in with the html a good idea? I feel that the code behind was meant to get rid of that.
jjnguy
The html generated with runat looks exactly like you would want to be in the page.
jjnguy