Hi this is really strange !!
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 inside any web page (and don't worry about the moral of this code, I made it just to show the idea) you'll get this error :
Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
Of course the error has nothing to do with the real problem, I searched for the code that was generated by asp.net and figured 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 from the asp page and this is the method that is meant to render our div (MainDiv), I found out that there is a missing bracket "}" that closes the method or the (for loop).
now the problem has three parts:
1- first you should have a server control (in our situation is the MainDiv) and I'm not sure if it is only the div tag.
2- HTML control inside the server control and a code inside it using the double quotation mark ( for example <div id="<%=str
instead of <div id='<%=str
.
3-Any keyword which has block brackets e.g.:for{},while{},using{}...etc.
now removing any part, will solve the problem !!!
how is this happening ?? any ideas ?
BTW: please help me to make the question more obvious, because I couldn't find the best words to describe the problem.
Look like my question is not clear !! My question is : what is the steps that asp.net follows to generate such a wrong code ?? how is that happening !! I don't want the solution for the exception , I already solved it using single quotation.
You can't enclose double quotes in double quotes.
<div id="<%=str.Replace("#","div") %>">
It can't tell when one is supposed to start or end which is why
<div id='<%=str.Replace("#","div") %>'>
would work.
Otherwise it thinks the code is
<div id="<%=str.Replace("
","
div
") %>"
)
which of course makes no sense.
Update based on my comment below
Adding the runat="server"
causes the control to be parsed by ASP.NET, otherwise it is just passed straight out as html.
If it gets parsed by ASP.NET things are different. To be honest I am not sure of the exact internal workings at play here. All I can tell you is that if the runat="server"
attribute is set then ASP.NET parses it and it plays by different rules.
It will try to decompose the inner html as I described above.
If the runat="server"
attribute is removed then it is passed out as html, hence the inline code is processed then just pushed out with the surrounding html, regardless of what it is, e.g. double quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With