views:

47

answers:

2

Hi,

Can someone explain the working principle of asp.Net below?

I have 2 separate code-block asp.Net expressions in an aspx markup, with an html content between (span element in the example below).

In the first code-block, there is "i" as an increment variable for the for loop.

Then the code-block is cut with an html content.

And another code-block expression is opened but as I see I can reach the "i" variable which was declared in the previous code-block.

So, how asp.net handles -compiles- the pieces of code-block experrions declared in the mark up? Does it check the semi-colons and generates some anonymous methods which will end up with many calls to Response.Write in the last place?

Thanks,

<p>
   <%for (int i = 0; i < 30; i++)
     {
         Response.Write("Some text here");

         %>

     <span> ______________________________ </span> <%--So how this line is processed 
                                                    by ASP.Net so that it is embedded 
                                                    in the for loop as Response.Write 
                                                    method's parameter?--%>

   <%
         Response.Write(i*(i+1));

         Response.Write("<br />");
     }%>
</p>
A: 

The line you refer to is not using the Response.Write call above it. It is simply embedded text in the web page.

The <span> tag is not a "guest" of the C# code; the C# code is a guest of the ASP web page (of which the <span> tag is a part).

Robert Harvey
+1  A: 

You are confusing the scope of the C# code with the inline ASPX markup. The ACTUAL code block is between the curly braces { } regardless of what is within that code block, in the case above the HTML code is still within the single block.

Just look for opening and closing curly braces, those define your code blocks, the % signs drop in and out of HTML / C# code.

Simon Lee
@Simon: SO in this case the rest -the content (like <span> element above) is all transferred ro Response.Write('what ever the content in markup'); as far as I understand.
burak ozdogan
@burak ozdogan - It would be instructive if you rewrote the entire piece as a single code execution without html interruption. Then you would see the portion of the code with `<span>___</span>` evaluates in this context as a `Response.Write("<span>___</span>);`. The `<%` blocks are not functional scope definitions.
Joel Etherton
@Simon Lee - +1 btw :)
Joel Etherton
Well, it all gets compiled to intermediary language so not quite the same as it creating a load of Response.Write statements but in theory yes, the end result is the same.
Simon Lee