Out of context String variable for use in HTML output

I am trying to understand why a variable myUrlis out of context in the example below. What is the best way to deal with this situation? Are there any alternatives? The code is C # on an ASP.NET page.

<% string myUrl = "http://www.website.com"; %>
<ul class="footerLinks">
    <li><a href="<%= myUrl %>/index.html">Home</a></li>
</ul>
+3
source share
2 answers

This is because it is <%=displayed before the script component. If you installed myUrlin the code behind ( Page_Loador the Initevent), it should enter the page as you expect. Obviously, also remove the variable declaration in the markup.

+2
source

First of all, your string variable should be publicly available at the class level.

public String myUrl

DataBind() method Page_PreRenderComplete:

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    DataBind();
}

<%= expressions .

+2

All Articles