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?
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.
vs
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.