Unit - V ( English Version )

Introduction to ASP.NET

ASP.NET is a web application framework developed by Microsoft for building dynamic websites, web applications, and web services. It is part of the .NET Framework and provides a robust and scalable way to create server-side web applications.

Features of ASP.NET

  1. Cross-Platform: Supports Windows, macOS, and Linux with .NET Core.

  2. State Management: Handles session and application states effectively.

  3. Security: Built-in support for authentication, authorization, and data validation.

  4. Rich Controls: Includes server-side controls for dynamic UI creation.

  5. Data Access: Simplifies database connectivity with technologies like ADO.NET.

  6. Performance: Uses compiled code for better performance compared to traditional interpreted frameworks.


ASP.NET Architecture

The ASP.NET architecture is based on the Common Language Runtime (CLR) and follows a layered structure:

  1. User Interface Layer:

    • Includes Web Forms and Razor Pages to define the user interface.

  2. Application Layer:

    • Contains business logic, such as handling requests and managing user interactions.

  3. Data Layer:

    • Manages database connections and data-related operations using ADO.NET or Entity Framework.

  4. HTTP Pipeline:

    • Processes HTTP requests and responses through modules and handlers.


Web Forms

Web Forms in ASP.NET are a page-centric programming model for creating dynamic and interactive web pages. They use a combination of HTML, CSS, and server-side code.

Structure of a Web Form:

  • Code-Behind File: Contains the server-side logic, written in C# or VB.NET.

  • ASPX File: Defines the UI using markup and server controls.

Example of a Simple Web Form:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyApp.Default" %>
<!DOCTYPE html>
<html>
<head>
    <title>Simple Form</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Enter Name: "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
    </form>
</body>
</html>

Web Servers

A web server hosts and delivers web applications to users. It processes HTTP requests and generates responses.

Common Web Servers for ASP.NET:

  1. IIS (Internet Information Services):

    • Default web server for ASP.NET applications on Windows.

  2. Kestrel:

    • Lightweight, cross-platform web server for .NET Core applications.

  3. Apache or Nginx:

    • Can be used as reverse proxies with Kestrel.


Server Controls in ASP.NET

Server controls are reusable components that simplify web development by handling both UI rendering and event handling on the server.

Types of Server Controls:

  1. HTML Controls:

    • Basic HTML elements enhanced with runat="server" attribute.

    <input type="text" runat="server" id="txtName" />
  2. ASP.NET Controls:

    • Predefined controls like TextBox, Label, and Button.

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  3. Data Controls:

    • Bind data to UI elements (e.g., GridView, DataList).

    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
  4. Validation Controls:

    • Validate user inputs (e.g., RequiredFieldValidator, RegularExpressionValidator).

Example of Validation Control:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox1" ErrorMessage="Field is required" runat="server" />

Data Connectivity Using ASP.NET

ASP.NET uses ADO.NET for connecting to databases and manipulating data. It supports both connected and disconnected models.

Connecting to a Database:

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection("your_connection_string");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    Console.WriteLine(reader["Name"]);
}

conn.Close();

Using Data Controls for Connectivity:

  1. GridView: Display data in tabular format.

  2. Repeater: Flexible data display.

  3. FormView: Provides detailed views of a single record.

Example with GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>

Introduction to XML

XML (eXtensible Markup Language) is a markup language used for storing and transporting data. It is widely used in ASP.NET for configuration and data exchange.

Features of XML:

  1. Platform Independent: Works across different systems.

  2. Hierarchical Structure: Data is organized in a tree-like structure.

  3. Self-Descriptive: Tags provide meaningful context.


Using XML with ASP.NET

XML is used in ASP.NET for various purposes:

  1. Web.config: Stores application settings, connection strings, and security configurations.

  2. Data Storage: Acts as a lightweight alternative to databases.

  3. Data Exchange: Facilitates communication between applications via SOAP or REST APIs.

Reading XML in ASP.NET:

using System.Xml;

XmlDocument doc = new XmlDocument();
doc.Load("data.xml");
XmlNodeList nodes = doc.SelectNodes("//User");

foreach (XmlNode node in nodes)
{
    Console.WriteLine(node["Name"].InnerText);
}

Writing XML in ASP.NET:

using System.Xml;

XmlDocument doc = new XmlDocument();
XmlElement user = doc.CreateElement("User");
user.SetAttribute("Id", "1");

XmlElement name = doc.CreateElement("Name");
name.InnerText = "John Doe";
user.AppendChild(name);

doc.AppendChild(user);
doc.Save("data.xml");

Summary

  1. ASP.NET: A robust framework for building web applications, leveraging web forms, and providing built-in server controls.

  2. Web Forms: Page-centric architecture for dynamic web pages with a mix of markup and server-side logic.

  3. Web Servers: Hosts ASP.NET applications, with IIS being the most common for Windows.

  4. Server Controls: Simplifies UI design and event handling in web forms.

  5. Data Connectivity: Uses ADO.NET for seamless integration with databases.

  6. XML in ASP.NET: Essential for configuration, data storage, and communication.

Last updated