I was wondering why do we have to wrap if statement with <%{%> And <%}%> for any if condition in aspx.
For example this code works:
<% if (contidtion) { %>
<%= DoSomething() %>
<%}%>
But this code doesn't work :
<% if (contidtion) { %>
<%= DoSomething()
}
%>
Can you please explain the difference between those 2 conditions. And why the first one does work and why the second one doesn't.
Looking at the definition of what the <%= %> tag does. It does the same thing as Response.Write().
https://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
Meaning, it will output whatever the evaluated expression is inside. If you do not close the tag before if's closing "}", the "}" is considered as part of the expression inside your Response.Write(). That is why it isn't considered a closing } for the if statement.
Related
I have a template engine, which uses <% and %> for templates, but problem is ASP.NET WebForms think other about that.
In Razor I can escape # symbol just by doubling it — ##. How do it in Web Forms?
UPD: HTML escaping not helps — template engine don't want to use <% and %>, so site just show's them.
I may have found some answers here: http://www.sitepoint.com/forums/showthread.php?65377-escape-asp-tags
text = "<" & "% CODE %" & ">"
<% CODE %>
Temporary solution is creating server-side variables and use like <%= OPEN %> and <%= CLOSE %>.
Currently if I have this:
<div>
some dynamic data
</div>
I am using
<div>
<% Response.Write(get.SomeString()); %>
</div>
Obviously this works fine, but there definitely seems like there should be a shorthand for this.
You're looking for <%: get.SomeString() %>
The basic syntax is documented on MSDN:
Code render blocks define inline code or inline expressions that execute when the page is rendered. There are two styles of code render blocks: inline code and inline expressions. Use inline code to define self-contained lines or blocks of code. Use inline expressions as a shortcut for calling the Write method.
<% inline code %>
<%=inline expression %>
In your case, that would look like:
<div>
<%= get.SomeString() %>
</div>
Alternatively, while not noted in the MSDN documentation (but mentioned in the comments), in newer versions of ASP.NET, you can also use <%: … %> syntax to automatically escape any HTML in before writing it to the output. As Scott Guthrie explains, this is an important step in guarding against certain forms of attacks. Which form you should choose will depend on your exact use case.
I'm using the Repeater control. All the property names in my data source are represented in my ImportColumns enum.
So I am trying to loop through the enums to pass the names to the Eval method.
This is in ItemTemplate section of my Repeater control.
Line 57: <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58: <% foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) { %>
Line 59: <td><%# Eval(column.ToString()) %></td>
Line 60: <% } %>
Line 61: </tr>
But line 59 gives me the following error.
CS0103: The name 'column' does not exist in the current context
Why doesn't the code recognize the column variable? I'm guessing it either has something to do with the fact that I'm in a repeater control or that I'm missing <% %> code and <%# %>, but it seems like this should work.
EDIT:
I see that if I change line 59.
<td><%= column.ToString() %></td>
There is no error. (Although it no longer does what I need.)
So this has something to do with Eval that prevents "regular" variables from working. Does anyone know of a workaround to this?
The column variable is not recognized because <%# ... %> data-binding expressions are evaluated when the container is data-bound, whereas <% ... %> and <%= ... %> code blocks are executed when the page is rendered. If you click "Show Complete Compilation Source" on the error page, you'll see that
foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) {
and
Eval(column.ToString())
are located in two separate methods.
One possible solution is to combine the loop and Eval into a single data-binding expression. For example:
<%#
string.Concat(Enum.GetValues(typeof(ImportColumns)).Cast<ImportColumns>()
.Select(column => "<td>" + Eval(column.ToString()) + "</td>"))
%>
I think what you are typing to do is use the name of the enumeration entries to be used as the property name for the Eval
If so then use the Enum.GetNames instead of the GetValues method.
Line 57: <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58: <% foreach (string column in Enum.GetNames(typeof(ImportColumns))) { %>
Line 59: <td><%# Eval(column) %></td>
Line 60: <% } %>
Line 61: </tr>
Edit:
You are right I didn’t test that. I also don’t use inline code so I don’t understand the intricacies. Here is a solution that works for me.
I would put that loop logic in a method in the code behind file that creates the complete row and then invoke it from the aspx page.
In the aspx
<tr class="ProductRow" data-id="<%# Eval("Id") %>">
<%# this.ColumnDetail() %>
</tr>
The method in the code behind
protected string ColumnDetail() {
string html = String.Empty;
foreach (string x in Enum.GetNames(typeof(ImportColumns))) {
html = (html + string.Format("<td>{0}</td>", Eval(x)));
}
return html;
}
Explain how this could print out two different values:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<Models.AModel>>" %>
<%
int i = -1;
foreach (var m in AModel){
i++;
%>
<td><%= Html.TextBox(string.Format("[{0}].TheName", i), m.TheName) %> <%= m.TheName %></td>
<% } %>
For example, if theList contains three elements {TheName: "A"} and {TheName: "B"} and {TheName: "C"}, it will print:
<td><input name="[0].TheName" type="text" value="A">B</td>
<td><input name="[1].TheName" type="text" value="B">C</td>
<td><input name="[2].TheName" type="text" value="C">A</td>
Totally baffled.
I'll give votes for guesses and answer to the best guess, even if it's not the answer. To start out:
-TheName does not have any special code in the getter.
EDIT: Clarified the question with better examples (as I've discovered them), polished up the code to match suggestions. Still have the problem. As you can see, it appears the two lists are somehow out of order.
Is this a partial dump of the code? There are a number of oddities:
You're using <%: with Html.TextBox, which doesn't make sense. <%: HTML-encodes output, so it doesn't make sense that you'd be using it to print a TextBox.
You've got a closing </td> tag in your loop, but no opening <td> tag.
Update:
After your edit, it just looks to me like your list/enumerable must be doing something bizarre (or your posted output isn't the actual output) You should show us how you're populating your model.
This code is not correct at all. Jacob has mentioned some of em.
Even if you are sure this code works then i think it has something to do with List item being bound to view and something happens that shows next value...
What am I doing wrong? How come <%= this %> isn't being interpreted as C#?
Here's the code :
And here is what it renders (notice the Firebug display):
What do you think is going on? MVC newb here. :(
And the static Site class:
(If you cannot see the screenshots on the page, view source and use the URLs from the <img> tags.)
<%: %> starts with .NET v4
For pre-v4 it's equivalent is <%= Html.Encode(...) %>
The problem was that I was using <%= %> (or even <%: %>) within a tag that had runat="sever".
Shouldn't that be <% %> or <%= %> for a shorthand of Response.Write?
Here's an MSDN article on Embedded Code Blocks.
This sometimes happens to me when embedding code inside of html attributes. I've never quite pinned down the exact cause but sometimes you can get around it by using single quotes rather than double.