2

I'm trying to do this simple task using this kind of coding (ASP.NET Inline expression):

Where:

Vars is a static class that contains static strings variables.

Code:

<asp:HyperLink ID="anyHyperLink" runat="server" />
<% anyHyperLink.NavigateUrl = Vars.aDirectoryString; anyHyperLink.Text = "Some Text"; %>

The output markup ends showing that the inline expression code has no effect!

Thanks..

1 Answer 1

2

You have to put that line above the control, not below it. The engine reads top to bottom, so by the time it gets to your code, the HTML has already been rendered for the link.

Or you can put it in the code-behind in Page_Load or something like that, instead of the markup.

Sign up to request clarification or add additional context in comments.

3 Comments

Aha thanks, You're right that there are better places where I can put the code, but I'm just improving my coding skills by this snippet, Thanks again
Why I can't write: <asp:HyperLink ID="anyHyperLink" NavigateUrl='<% Vars.aDirectoryString %>' runat="server" />?
Not sure why not - it's just one of the weird behaviors of ASP.NET WebForms (one of a hundred reasons I stopped using WebForms in favor of MVC). You might be able to do a <%# %> but then the control would have to be data-bound to something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.