开发者

String variable out of context for use in HTML output

I'm trying to understand why the variable myUrl is out of context in the example below. What's the best way to handle this situation? Are there alternatives? The code is C# in an ASP.NET page.

<% string myUrl = "http://www.website.com"; %>
<ul class="footerLinks">
    <li开发者_StackOverflow><a href="<%= myUrl %>/index.html">Home</a></li>
</ul>


It's because the <%= is being rendered before the script component. If you set myUrl in the code behind (Page_Load or Init event) then it should come through into the page as you are expecting. Obviously, remove the variable declaration in the markup as well.


First of all your string variable should be set to public at the Class level.

public String myUrl

Then you need to call the DataBind() method in the Page_PreRenderComplete event:

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

Because <%= expressions are evaluated at render time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜