8

When I use an anchor tag on an aspx page as below,

<a href="~/pages/page.aspx?id=<%= ServervariableName %>"> test </a>

it will get the variable value correctly assigned to id but it won't route the page correctly as the ~ will not be evaluated without the runat="server" attribute on the 'a' tag. But once I add the runat server attribute, it does not evaluate the servervariable name anymore.. Does anyone know how this works or what I should do to take care of both?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
user479843
  • 85
  • 1
  • 3
  • 8

4 Answers4

13

Try the following:

href="<%= ResolveUrl("~/pages/page.aspx") + "?id=" + ServervariableName %>"

This will only work if you do not add runat="server".

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • 2
    @Letseatlunch - Thank you. Updated the answer. To be honest: it's been years since I've developed asp.net. Thought I'd try an answer. I'll just stay out of the `asp.net` tag I think :D. – Pieter van Ginkel Nov 29 '10 at 18:43
  • Yes, forgot about the resolveurl function. Thank you very much, it worked and is exactly what i was looking for. Its a menu item that needs to pass in a value dynamically.. so..! – user479843 Nov 29 '10 at 18:52
  • Heres a create work around if you ever need to use resolveUrl in a non "Page/Control" context: www.codeproject.com/KB/aspnet/resolveurl.aspx – Letseatlunch Dec 05 '10 at 00:32
2

You cannot use this syntax to set properties of server-side controls.

Instead, you can set the property in the code-behind, or use data-binding syntax (<%# expression %>) and call DataBind() in the code-behind.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

If you want both runat=server and a virtual path you either of:

  1. Set the href in your code behind. Afaik MapPath doesn't remove query strings.
  2. If you don't want to use MapPath, you can use VirtualPathUtility which MapPath uses internally most likely.
  3. If you want to bind the variable in your .aspx-file and still want runat="server", then you have to use an expression builder. You syntax then becomes

    <a href="~/folder/page.aspx?id=<%$ MyVars:ServerVariableName %>" runat="server">The link</a>

PM me if you need help with it. It's quite useful for customizing localization and for interfacing the ASP.Net compiler (i.e. not the C#/VB one, but the one that can compile ASPX-pages)

Henrik
  • 9,714
  • 5
  • 53
  • 87
  • The expression builders is a really cool which I didn't know about. Thanks for letting me know. Is there an easy way to declare a page variable and convert that to an expression? – user479843 Nov 29 '10 at 19:23
  • By a "page variable", what do you mean? A protected field of the class that the page compiles to? In fact, what the expression builder does is invoked by the CodeDom at ASP.Net's compile time and the implementation lets you emit any code you want. I'm pretty you could make the MyVars-expression builder emit the code to access a field, although I haven't done it. – Henrik Dec 01 '10 at 14:17
-2

I'm not exactly sure why runat="server" prevents the evaluation of <%= Property =>, but you need runat="server" to evaluate the ~ root. According to MSDN,

You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.

Anyways, this might get you going.

ASPX

<asp:HyperLink ID="myHyperLink" runat="server" NavigateUrl="~/MyPage.aspx?id=<%=MyProperty %>" Text="Click Me" />

CS

myHyperLink.NavigateUrl = string.Format("~/MyPage.aspx?id={0}", MyProperty);
bitxwise
  • 3,534
  • 2
  • 17
  • 22