Next


Advantages of ASP.NET

 Offers very much easier designing.
 Automatically generates the design tags, when you drag and drop the controls from the toolbox.
 While you type the tags manually, it displays the list of attributes and possible values automatically.
 Supports “Post-backing”.
 In ASP.NET 3.5, “AJAX” is also introduced.
 Avoids repeated design in the web pages, with the concept of “Master Pages”.
 Avoids the usage of “JavaScript”, with “Validation Controls”. The validation controls are used to perform client side validations.
 It supports “Crystal reports” in the web sites. There is no other server side programming language, that supports “Crystal reports” in the web sites.
 Supports “.asp” pages and “.aspx” pages. Note: The server page file extension is “.aspx”.
 Supports both html controls and server controls.

Files of ASP.NET Server Page
 .aspx file:
 Contains the designer code. (with html tags). This is similar to “.designer.cs” file in windows applications.

 .aspx.cs file:  Contains the executable code of the web page. (Event handlers). This is similar to “.cs” file in windows applications.
ASP.NET Server Page Definition Syntax

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

 In the above code, the <%@ Page> tag provides the information about the type of language, name of the code file and the name of the class behind the web page.
 The <!DOCTYPE> is a optional tag, which contains the information about the current version of xhtml and the url of “dtd” file, available in “w3.org” site. The “dtd” file offers the rules and regulations of xhtml tags.
 The <html%gt tag specifies the starting and ending point of html code.
 The <head> tag contains the header information of the web page. It may contain <title>, <script> and <style> tags.
 The <body> tag contains the actual content of the web page. In ASP.NET server page, it should contain at least one <form> tag.
 The <form> specifies the starting and ending point of the form. It should contain an attribute called runat=”server”, which enables the form to run at server mode.
 The <div> tag specifies the alignment of the web page content. This is also an optional tag.

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

 In the above code, the importing section used to import the necessary library namespaces.
 The class definition, creates a class for the web page. It is created as “public” class. It is created as “partial” class. It inherits the predefined class called “System.Web.UI.Page”.
 The Page_Load is the event handler for “Load” event. Note: The event handlers in ASP.NET, are to be created as “protected” methods.

Development of Traditional Web Sites in ASP.NET

 Create two web pages.
 Client page (with .html extension)
 Server page (with .aspx extension)
 Create a form in the client page with the reference of the server page and also with a “submit” button. At run time, whenever the user clicks on that submit button, automatically the server page will be opened based on the reference URL.

Demo on Traditional Web Sites Development

myclientpage.html (To add this page, click on “Web Site” menu – “Add New Item” – “HTML Page”.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Client Page</title>
</head>
<body>
    <form action="myserverpage.aspx" method="post">
        <input type="submit" />
    </form>
</body>
</html>
myserverpage.aspx (To add this page, click on “Web Site” menu – “Add New Item” – “Web Form”.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myserverpage.aspx.cs" Inherits="myserverpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My Server Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>The server page invoked.</h1>
    </div>
    </form>
</body>
</html>


<head runat="server">
    <title>My Server Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>The server page invoked.</h1>
    </div>
    </form>
</body>
</html>


Demo on Traditional Web Sites Development (with Parameters)

myclientpage.html (To add this page, click on “Web Site” menu – “Add New Item” – “HTML Page”.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Client Page</title>
</head>
<body>
    <form action="myserverpage.aspx" method="post">
       <div align="center">
           <h1>Login</h1>
            User Name: <input type="text" name="txtUserName" /><br />
            Password: <input type="password" name="txtPassword" /><br />
            <input type="submit" />
       </div>
    </form>
</body>
</html>
myserverpage.aspx (To add this page, click on “Web Site” menu – “Add New Item” – “Web Form”.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myserverpage.aspx.cs" Inherits="myserverpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My Server Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <h1 align="center">Welcome to the server page.</h1>
    </div>
    </form>
</body>
</html>
myserverpage.aspx.cs
public partial class myserverpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username, password;
        username = Request.Form["txtUserName"];
        password = Request.Form["txtPassword"];
        if (username == "system" && password == "manager")
            Response.Write("<h1 align=center>Valid Login.</h1>");
        else
            Response.Write("<h1 align=center>Invalid Login.</h1>");
    }
}