1

I am learning some aspx and had a question. I have the following example code:

<%@ Page Language="C#" %>
<html>
<body>
    <form id="form1" runat="server">
       Current server time is <% =GetTime()%>.
    </form>
</body>
</html>

Now what i dont understand here is that if the form is running at server already, why do we need the <% %> code blocks again? Maybe im not understanding the distinction between the code blocks and the whole running at server on the page. Can anyone please clear up my misconceptions about what is happening here? I just dont really understand the serverside vs clientside terminology for an aspx page.

I have also seen code like this:

<script language="VB" runat="server">
Dim ....

Public Sub PageLoad()
...
Response.Write("Hello")
End Sub
</script>

<% If dataExists(whatever) Then %>
HelloWorld

Now why couldnt the codeblocks be eliminated above? I mean in the server tags, that code is already running server side and making some write requests. Then you have code blocks which are doing some logic check to write again. Why separate these? Arent they the same functionality or am i missing something?

John Baum
  • 3,183
  • 11
  • 42
  • 90

6 Answers6

2

Let's make clear a thing from start. You are not learning aspx, unless you're trying to find out about this file extension, you're learning ASP .Net and more specific ASP .Net Web Forms.

The markup from the Web Forms (the code in the aspx file) is parsed by ASP .Net, then a class will be generated, which is going to be used later to actually create the HTML.

This markup can contain pure html, server controls (<asp:Label />, etc), code blocks, DataBinding expressions, resources expressions and many more.

All these are just parsed to obtain that generated class. The generated class will contain more or less instructions for a writer to write some strings (html if you want).

Probably you've seen this example and now you can find out this is some kind of syntax sugar.

 <% { Response.Write(DateTime.Now.ToString()); }%>

vs

 <%= DateTime.Now.ToString() %>

It produces the same thing, but is written in two different ways. That syntax is just parsed.

There is only one difference between server code and client code: server code runs on server and client code runs on client. That client code is (or might be) produced by the server so the client can run it on its side.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
0

<% ... %> blocks denote framework code (in this example C#), while the rest of the document is pure HTML.
So blocks are used to distinguish between code and simple markup.

Marco
  • 56,740
  • 14
  • 129
  • 152
0

When you add runat="server" to an html element, it turns it into a server side object. Which means that in the Designer file an HtmlForm instance is created. What is inside the tags isn't included, unless there are other server side controls which will be added to the control tree. Inside the tags you're back to writing plain Html, until you put something else for .Net to pay attention to, <% =GetTime()%> for example.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
0

Code blocks simply allow the processing engine to know which parts of the page to execute (without then it would be much harder to tell which parts of the page were code and which were simply markup).

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

The code blocks are the way to delineate strict HTML from C# code.

If you wrote

<form id="form1" runat="server">
    Current server time is GetTime().
</form>

then you would get the text verbatim. It is only when you add the code blocks that the parser understands to switch to C# code for that particular section of the HTML.

Rather than re-write an already good answer: Here is the SO question as to what the runat server is for

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0

In a nutshell, the browser requests the page from your server, ASP.NET then processes any executable code i.e. any code within <% ... %> blocks, the result of that is then sent back to the browser.

Another way of looking at it is any code you want to run server side should be within <% ... %> blocks, everything else is client side.

James
  • 80,725
  • 18
  • 167
  • 237