Tutorials » AspNet tutorials »

Your first dynamic page


You will learn to create a dynamic page in this chapter.


In an earlier chapter, you learned how to create a small Shopping Cart web site with a simple HTML page in it. In another chapter you larned how to convert the simple html page to an ASP.NET page. That was fun! You simply renamed the html page to an aspx page.

In this chapter you are going to do some real work by writing server side code, which make it a real dynamic page.

To write server side code, you must choose a .NET language. It can be any .NET language like C#, VB.NET, C++.NET or J#. For this chapter, let us choose VB.NET.

Go back to your index.aspx page in your folder "C:\ShoppingCart\". Open the file using notepad. You can see the following content in the index.aspx file.


<html>

<head>
<title>This is my first web page</title>
</head>

<body>

<br><br><br><br>

<center>

<font size=5 color=red>Welcome to SpiderShop !</font>

<BR><BR>

<font size=3 color=darkgreen>Buy softwares and tools online at very low rates....</font>

</center>

</body>
</html>


As you can see, it is plain HTML even though the page is an aspx page. Yes, you can have plain HTML inside an ASP.NET page. In this case, there is no server side code and the server has no processing to do, other than simply returning the HTML content as it is, to the browser. Only when there is some server side code is present in the code, the server will process it and return the output embedded in other HTML content.

How to write server side code in a page ?



Our index.aspx page has only plain HTML now. We are going to embed some server side code inside it so that the server will process it and generate some output dynamically.

Anything you write inside a special tag as shown below will be trated as server side code:

<%

%>


You can write your favourite C# or VB.NET code inside the <% %> tags and it will be processed by the .NET runtime to generate html output.

Change the content of your index.aspx as shown below:



<html>

<head>
<title>This is my first web page</title>
</head>

<body>

<br><br><br><br>

<center>

<font size=5 color=red>Welcome to SpiderShop !</font>

<BR><BR>

<font size=3 color=darkgreen>Buy softwares and tools online at very low rates....</font>

<br><br>

Now the server time is :
<%
Response.Write (DateTime.Now.ToString)
%>

</center>

</body>
</html>


Now access your page using the URL http://localhost/ShoppingCart/Index.aspx. You will see the following output in browser:





Buy softwares and tools online at very low rates....

Now the server time is :
4/28/2005 6:33:45 PM



You can see the actual server time displayed in your browser. (Since you are using your own machine as the server and client, you will see your computer time. )

Let us see how it worked. You have writtena piece of code as shown below:

Now the server time is :
<%
Response.Write (DateTime.Now.ToString)
%>

Since this page has an extension .aspx, the page will be given to the aspnet service to process it. The aspnet service will use the .NET runtime to process all lines enclosed between <% and %>.

In the above case, there is only one line of server side code.

Response.Write (DateTime.Now.ToString)


The code Response.Write is used to generate output.

DateTime.Now.ToString will return the current date and time.

So, the resule of the following code is the current date and time and Response.Write() will return that result to the asp.net page which embeds the code.

In the page, we have

Now the server time is :
<%
Response.Write (DateTime.Now.ToString)
%>


In the above lines, Now the server time is : is a plain HTML. The next part gets executes in the server and returns the current time. So, the final output is:


Now the server time is :
4/28/2005 6:33:45 PM


What a browser gets back from the server is just the above lines. Your server side code is not there anymore. It is processed by the server to produce some output!

Now you must have got a better picture about how the server side code works.

In the coming chapers, we will explain how to write advanced code to write a more dynamic page, which access database and retrieve content just like this Tutorial page.




Next Chapter: Default programming language for ASP.NET
Previous Chapter: Client side scripting and server side scripting
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
TodayLast 7 Daysmore...

Awards & Gifts

Online Members

Williams
More...
 
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India