Monday, December 31, 2012

Introduction to ASP.NET

Introduction to ASP.NET

   ASP.NET is more than the next version of Active Server Pages (ASP); it provides a unified Web development model that includes the services necessary for developers to build enterprise-class Web applications. 
     While ASP.NET is largely syntax compatible with ASP, it also provides a new programming model and infrastructure for more scalable and stable applications that help provide greater protection.
    You can feel free to augment your existing ASP applications by incrementally adding ASP.NET functionality to them.
     ASP.NET is a compiled, .NET-based environment; you can author applications in any .NET compatible language, including Visual Basic .NET, C#, and JScript .NET. Additionally, the entire .NET Framework is available to any ASP.NET application. 
     Developers can easily access the benefits of these technologies, which include the managed common language runtime environment, type safety, inheritance, and so on.

ASP.NET includes:
  • A page and controls framework
  • The ASP.NET compiler
  • Security infrastructure
  • State-management facilities
  • Application configuration
  • Health monitoring and performance features
  • Debugging support
  • An XML Web services framework
  • Extensible hosting environment and application life cycle management
  • An extensible designer environment

    Page and Controls Framework

    The ASP.NET page and controls framework is a programming framework that runs on a Web server to dynamically produce and render ASP.NET Web pages. ASP.NET Web pages can be requested from any browser or client device, and ASP.NET renders markup (such as HTML) to the requesting browser. As a rule, you can use the same page for multiple browsers, because ASP.NET renders the appropriate markup for the browser making the request. However, you can design your ASP.NET Web page to target a specific browser, such as Microsoft Internet Explorer 6, and take advantage of the features of that browser. 
     

    ASP.NET Compiler

    All ASP.NET code is compiled, which enables strong typing, performance optimizations, and early binding, among other benefits. Once the code has been compiled, the common language runtime further compiles ASP.NET code to native code, providing improved performance.
     


Sunday, December 30, 2012

Changing the themes of page on Button Click

Changing the themes of page on Button Click

<%@ Page Language="C#" AutoEventWireup="true" Theme="Green" 
           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></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    

    <asp:Button ID="btn1" runat="server" Text="Black" OnClick="Button1_Click" />

     <asp:Button ID="Button1" runat="server" Text="Green" OnClick="Button2_Click" />

    </div>

    </form>

</body>

</html>


Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {



    }

    public static string theme;

    protected void Page_PreInit(object sender, EventArgs e)

    {

        switch (Request.QueryString["theme"])

        {

            case "Black":

                Page.Theme = "Black";

                break;

            case "Green":

                Page.Theme = "Green";

                break;

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Redirect("Default.aspx?theme=Green");

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        Response.Redirect("Default.aspx?theme=Black");

    }

}

 

3-Tier architecture

3-Tier architecture is one of the most popular approaches in software development.


1.gif

Tier Vs Layer

Tier indicates a physical separation of components.

Here separate assemblies/services are made to represent each component.

Layer indicates a logical separation of components with the help of namespaces and classes

Components of 3-Layer Architecture

  • Presentation Tier: in simple terms, the UI with which the end user actually interacts.
  • Data Tier: Where actual data is stored.
  • Business Tier: It acts a bridge between Data Tier and the Presentation Tier. It collects raw data from the Presentation Tier, checks for validations, converts them to a standard format and finally sends them to the Data Tier. Similarly it collects data from the Data Tier, purifies it and sends it to the Presentation Tier for display.
  • Business Objects: Its nothing but a class representation of actual data.
How it Looks
2.gif

Code Walkthrough
What we are developing.

We are developing a customer screen which supports 2 operations:

  1. Add New Customer
  2. List All Customers.
Now customers may be of the types

Gold, Silver or Normal which represents Id as 1, 2 and 3 respectively.

Note: In the database CustomerTypeId will be stored but in the grid the descriptive text will be shown.

Step 1: Create Business Objects required - we need 2 things,

one representing the customer type and anotehr representing the customer itself.

public class ClsCustomerType
{
       public ClsCustomerType(int id)
       {
             this.Id = id;
             switch(Id)
             {
                    case 0:
                    this.Name = "--Select--";
                    break;
                    case 1:
                    this.Name = "Gold";
                    break;
                    case 2:
                    this.Name = "Silver";
                    break;
                    case 3:
                    this.Name = "Normal";
                    break;
             }
       }
       public int Id
       {
              get;
              set;
        }
        public string Name
        {
              get;
              set;
         }
}
public class ClsCustomer{
        public string CustomerCode
        {
               get;
               set;
        }
        public string CustomerName
        {
               get;
               set;
        }
        public ClsCustomerType CustomerType
        {
                get;
                set;
         }
    }
}

Step 2: Create Business Access Layer

public class ClsCustomerBAL{
        public List<ClsCustomerType> LoadCustomerTypes()
        {
               return new List<ClsCustomerType>()
               {                                   
                       new ClsCustomerType(0),new ClsCustomerType(1),new ClsCustomerType(2),new ClsCustomerType(3)
               };
        }       
        public IEnumerable<ClsCustomer> LoadCustomers()
        {
               DataTable dtCustomer=new ClsCustomerDAL().LoadCustomers();

3-Tier architecture is one of the most popular approaches in software development.

1.gif

Tier Vs Layer

Tier indicates a physical separation of components.

Here separate assemblies/services are made to represent each component.

Layer indicates a logical separation of components with the help of namespaces and classes

Components of 3-Layer Architecture

  • Presentation Tier: in simple terms, the UI with which the end user actually interacts.
  • Data Tier: Where actual data is stored.
  • Business Tier: It acts a bridge between Data Tier and the Presentation Tier. It collects raw data from the Presentation Tier, checks for validations, converts them to a standard format and finally sends them to the Data Tier. Similarly it collects data from the Data Tier, purifies it and sends it to the Presentation Tier for display.
  • Business Objects: Its nothing but a class representation of actual data.
How it Looks
2.gif

Code Walkthrough
What we are developing.

We are developing a customer screen which supports 2 operations:

  1. Add New Customer
  2. List All Customers.
Now customers may be of the types

Gold, Silver or Normal which represents Id as 1, 2 and 3 respectively.

Note: In the database CustomerTypeId will be stored but in the grid the descriptive text will be shown.

Step 1: Create Business Objects required - we need 2 things,

one representing the customer type and anotehr representing the customer itself.

public class ClsCustomerType
{
       public ClsCustomerType(int id)
       {
             this.Id = id;
             switch(Id)
             {
                    case 0:
                    this.Name = "--Select--";
                    break;
                    case 1:
                    this.Name = "Gold";
                    break;
                    case 2:
                    this.Name = "Silver";
                    break;
                    case 3:
                    this.Name = "Normal";
                    break;
             }
       }
       public int Id
       {
              get;
              set;
        }
        public string Name
        {
              get;
              set;
         }
}
public class ClsCustomer{
        public string CustomerCode
        {
               get;
               set;
        }
        public string CustomerName
        {
               get;
               set;
        }
        public ClsCustomerType CustomerType
        {
                get;
                set;
         }
    }
}

Step 2: Create Business Access Layer

public class ClsCustomerBAL{
        public List<ClsCustomerType> LoadCustomerTypes()
        {
               return new List<ClsCustomerType>()
               {                                   
                       new ClsCustomerType(0),new ClsCustomerType(1),new ClsCustomerType(2),new ClsCustomerType(3)
               };
        }       
        public IEnumerable<ClsCustomer> LoadCustomers()
        {
               DataTable dtCustomer=new ClsCustomerDAL().LoadCustomers();
        }                                   
        public void SaveRecord(ClsCustomer objCustomer)
        {
                new ClsCustomerDAL().SaveRecord(objCustomer);
         }
         public ValidationResult ValidateCustomerCode(string strPriCustomerCode)
         {
                if(strPriCustomerCode.Trim() == string.Empty)
                {
                        return ValidationResult.Empty;
                 }
                 else if(strPriCustomerCode.Trim().Length < 5)
                 {
                         return ValidationResult.InValid;
                  }
                  return ValidationResult.Valid;
         }
}

Step 3:  Next we need a Data Access Layer:

using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
      public class ClsCustomerDAL      {
             private const string ConnectionString = @"Data Source=SUKESH-PC\S2;Initial Catalog=3LayerApp;Persist
Security Info=True;User ID=sa;Password=aa"
;
             public DataTable LoadCustomers()
             {
                    //Load Data From Database
              }
              public void SaveRecord(ClsCustomer objCustomer)
              {
                    //Save Data Into Database
              }                
       }
}

Step 4: That's it; now the only thing remaining is creation of the UI:

3.gif

3-Tier architecture is one of the most popular approaches in software development.

3-Tier architecture is one of the most popular approaches in software development.


In the UI the code behind we will just call the functions created on BAL, there will not be any database logic, and neither wll there be validation logic.

Hope this explanation was enough to understand, how to create a simple 3-Layer application using ASP.Net also I hope you enjoyed reading this article.

        }                                   
        public void SaveRecord(ClsCustomer objCustomer)
        {
                new ClsCustomerDAL().SaveRecord(objCustomer);
         }
         public ValidationResult ValidateCustomerCode(string strPriCustomerCode)
         {
                if(strPriCustomerCode.Trim() == string.Empty)
                {
                        return ValidationResult.Empty;
                 }
                 else if(strPriCustomerCode.Trim().Length < 5)
                 {
                         return ValidationResult.InValid;
                  }
                  return ValidationResult.Valid;
         }
}

Step 3:  Next we need a Data Access Layer:

using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
      public class ClsCustomerDAL      {
             private const string ConnectionString = @"Data Source=SUKESH-PC\S2;Initial Catalog=3LayerApp;Persist
Security Info=True;User ID=sa;Password=aa"
;
             public DataTable LoadCustomers()
             {
                    //Load Data From Database
              }
              public void SaveRecord(ClsCustomer objCustomer)
              {
                    //Save Data Into Database
              }                
       }
}

Step 4: That's it; now the only thing remaining is creation of the UI:

3.gif

In the UI the code behind we will just call the functions created on BAL, there will not be any database logic, and neither wll there be validation logic.

Hope this explanation was enough to understand, how to create a simple 3-Layer application using ASP.Net also I hope you enjoyed reading this article.