views:

168

answers:

1

In code-behind of an ASP.NET page I have this method:

public string TestFunc()
{
    return "http://www.blabla.net";
}

And in markup this:

<%= TestFunc() %>

If I run the page I see "http://www.blabla.net" as text on the page and in debugger I enter TestFunc - all as expected.

If I put this Hyperlink on the same aspx page...

<asp:HyperLink ID="MyLink" runat="server" NavigateUrl='<%= TestFunc() %>'>Proceed...
</asp:HyperLink>

... I don't enter TestFunc in debugger and the generated HTML of the href contains the embedded code simply as text: href="%3C%=%20TestFunc%28%29%20%%3E"

What am I doing wrong here? I have already tried stupid trial and error: Replaced = by #, omitted = and replaced single quotation marks ' by double quotation marks ". But all this didn't help. Now I am stumped.

Thanks for help in advance!

+1  A: 

You can use "<% %>" construction within container which supports templates (e.g. GridView with combination of Bind, Eval). Simple usage with properties will cause encode your expression and will display "as is".

sashaeve
But why does then the first simple example in my question without the HyperLink work? It's not in any asp container.
Slauma
Such situation is also allowed.
sashaeve
Ah OK, I understand. Control properties (outside of container templates) are the problem.
Slauma
OK, I have found 2 workarounds for my purpose. 1) In Page_Load: `MyLink.NavigateUrl = TestFunc();` or 2) `<a href="<%= TestFunc() %>">Proceed...</a>`
Slauma