In asp.Net, writing code in the control tag generates compile error

Posted by Nour Sabouny on Stack Overflow See other posts from Stack Overflow or by Nour Sabouny
Published on 2012-03-22T11:09:49Z Indexed on 2012/03/22 11:29 UTC
Read the original article Hit count: 352

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about compiler