How to escape this ViewData in label?
I always heard the benefit of making html helpers but no one really s开发者_C百科hows how it would look if you did not right an html helper.
So I just wanted to write a label with plan html and not my html helper I made
<label for="<% ViewData["View_CoursePrefix"].ToString(); %>"></label>
I am trying to put a viewData in the "for" part but it keeps saying semi colon expected. I tried to escape the quotes but that does not work. What do I need to do to make this work?
It's the difference between <% %>
& <%= %>
When you write:
<%= ViewData["View_CoursePrefix"] %>
Behind the scenes, the WebForms view engine translates it to:
<% Response.Write(ViewData["View_CoursePrefix"]); %>
So to get your version to work, you'd have to wrap it in Response.Write()
.
HTHs,
Charles
Oh, and FYI... In ASP.NET 4.0 they're introducing <%: %>
which will automatically HtmlEncode the output. See this blog post from Phil Haack
<label for="<%=ViewData["View_CoursePrefix"] %>">some text</label>
精彩评论