[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
ASP-Basics

Creating Websites with ASP .NET using C#

This tutorial shows how ASP .NET works in general and how to create websites in ASP .NET using C#.

HTML vs Code behind

If you have experience with other scripting languages like JavaScript, PHP or Perl, the first thing you need to learn about ASP is, that ASP scripts are executed on the server side and not on the client side.

Another thing you need to learn is, when using ASP .NET, that there are 2 types of code. The first type of code is the normal HTML code that the user will see when clicks the right mouse button in the browser. The 2nd type of code is called code behind which is the C# source code. Note that the user only sees the HTML code, not the C# code. This is why websites written ASP .NET are more secure than other websites written in JavaScript etc.

Loading a page into the Browser

ASP websites are executed on the server side, and each website has a so called Load_Page function that uploads the site into the browser. Also you can use this functin to execute basic site initializations, like assigning variables, label texts, etc.

<script runat="server">
//display the userHost address in the browser
void Page_Load(object sender, EventArgs e)
{
  string userHost = Request.UserHostAddress;
  Response.Write(userHost);
}
The result of this code should be 127.0.0.1.


Request and Respond

The code snippet above demonstrates how an ASP .NET website uploads itself into the browser and display some date on it.

It declares a string variable userHost that holds the name of the users host. Then, we use the Request object to display information on the website and use the predefinded property UserHostAddress to get this information. Finally, we use the Response object to use its write method to display the users host address.

The Request and Respond objecs are of overall importance when processing or displaying data.

Getting information about the user

One of the main tasks when writing ASP driven websites is, to read and retrieve information about the user. ASP allows us to read almost any information of the user: the language he speaks, the browser he is using, connection type, host, encoding, cookies etc.

Getting the URL string

void Page_Load(object sender, EventArgs e)
{
  lblURLInfo.Text = Request.URL.ToString();
}


This code displays the current URL in the URLInfo label.

Reading and Writing Cookies

void Page_Load(object sender, EventArgs e)
{
  foreach(string name in Request.Cookies)
   {
     HttpCookie cookie = Request.Cookies[name];
     Response.Write(cookie.name);
     Response.Write(" = ");
     Response.Write(cookie.Value);
     Response.Write("<br>");
   }
}


This script reads all cookis on the user host and displays their name and value.

Processing Query String information

The Query String holds all information and variables that are passed when a website is uploaded into the browser. Sometimes it may be important to know what happens when a website is loaded into the browser.

void Page_Load(object sender, EventArgs e)
{
  Response.Write("<h1>Processing Query String information</h1>");
  foreach(string name in Request.QueryString)
   Response.Write(name + " = " + Request.QueryString[name] + "<br");
}


This code may look a bit confusing but is actually easy to understand. In the first line we simply print a Header using normal HTML. Then, we declare a string variable name to store all QueryString information in it by using a foreach loop. Finally we display this information on the website using the Response objects write method again.



last edited (July 13, 2004) by athomas, Number of views: 10290, Current Rev: 2 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.