In asp.Net, writing a code in a control tag generates a compilation error

Hi, this is really weird!

But look at the following asp code:

<div runat="server" id="MainDiv"> <%foreach (string str in new string[]{"First#", "Second#"}) { %> <div id="<%=str.Replace("#","div") %>"> </div> <%} %> </div> 

Now, if you put this code on any web page (and don’t worry about the morality of this code, I did it just to show this idea) you will get this error:

Compiler Error Message: CS1518: expected class, delegate, enumeration, interface or structure

Of course, the error has nothing to do with the real problem, I looked for the code generated by asp.net and found out the following:

  private void @__RenderMainDiv(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) { @__w.Write("\r\n "); #line 20 "blabla\blabla\Default.aspx" foreach (string str in new string[] { "First#", "Second#" }) { #line default #line hidden @__w.Write("\r\n <div id=\""); #line 22 "blabla\blabla\Default.aspx" @__w.Write(str.Replace("#", "div")); #line default #line hidden @__w.Write("\">\r\n "); } 

This is the code that was generated on the asp page, and this is the method that is intended to render our div (MainDiv), I found that there is no bracket "}" that closes the method or (for the loop).

Now the problem consists of three parts:
1- first you should have server control (in our situation, MainDiv), and I'm not sure if this is just a div tag.
2- HTML control inside the server control and the code inside it using the double quote mark (for example, <div id="<%=str instead of <div id='<%=str .
3-Any keyword that has block brackets, for example: for {}, and {}, using {} ... etc.

Now removes any part, solves the problem !!!

How does this happen? any ideas?

By the way: please help me make the question more obvious, because I could not find the best words to describe the problem.

It seems my question is incomprehensible! My question is: what are the steps that asp.net should use to generate such incorrect code? How does this happen!! I do not want a solution for the exception, I already decided to use a single quote.

+6
source share
5 answers

You cannot enclose double quotes in double quotes.

 <div id="<%=str.Replace("#","div") %>"> 

He cannot say when to start or end, therefore

 <div id='<%=str.Replace("#","div") %>'> 

will work.

Otherwise, he considers the code

  <div id="<%=str.Replace(" "," div ") %>" ) 

which, of course, does not make sense.

Update based on my comment below

Adding runat="server" leads to the analysis of the ASP.NET control, otherwise it is simply passed as html.

If it understands ASP.NET, everything is different. Honestly, I'm not sure about the exact inner workings of the game. All I can tell you is that if the runat="server" attribute is set, then ASP.NET parses it and plays by different rules.

It will try to decompose the internal html, as I described above.

If the runat="server" attribute is removed, it is transmitted as html, so the embedded code is processed, and then it is simply forced out with the surrounding html, regardless of what it is, for example. double quotes.

+6
source

This worked for me:

 <div runat="server" id="MainDiv"> <%foreach (string str in new string[]{"First#", "Second#"})%> <%{%> <% string str1 = str.Replace("#","div"); %> <div id='<%= str1%>'>123</div> <%}%> </div> 
0
source

I'm not sure that you can fully understand why the parser does what it is, but from the last real line of code you can see that it has its own panties.

@__ w.Write ("\"> \ r \ n ");

This prints "> , not just is the last chevron in this expression

 <div id="<%=str.Replace("#","div") %>"> 

So, at this moment, the parser believes that it is in the line, but does not find the terminator, therefore it fails - this means that the last

<%}%> is never parsed - and your problem arises.

I would suggest that this is because the parser works in a linear fashion, and only when a missing line terminator is "detected" does a failure occur.

0
source

This is weird and it is probably a problem using this type of syntax with webforms, but I was able to get this code to compile and render divs into a page:

 <div runat="server" id="MainDiv"> <%foreach (string str in new string[] {"First#", "Second#"}) {%> <div id='<%= string.Format("{0}", str.Replace("#","div")) %>'> Testing </div> <%}%> </div> 
0
source

use single quotes instead of double

0
source

Source: https://habr.com/ru/post/911400/


All Articles