Summary - ASP.
NET
Unit 1: Introduction to .NET and ASP.NET
What Does .NET Represent?
NET stands for Network Enabled Technology.
Combines Object-Oriented Programming with internet-based application
development.
Framework Overview
A framework is software that integrates multiple technologies for application
development.
.NET Framework:
Developed by Microsoft, first beta version released in 2000.
Used to develop applications for web, Windows, and mobile platforms.
Supports over 60 programming languages, e.g., C#, VB.NET, Python, etc.
.NET Framework Architecture
Components:
1. BCL (Base Class Library):
Basic building block of .NET programs.
Provides pre-defined classes for application development.
2. CLR (Common Language Runtime):
Heart of the .NET Framework.
Converts MSIL (Microsoft Intermediate Language) code to native code.
Components of CLR:
JIT Compiler: Converts MSIL to native code at runtime.
Garbage Collector: Frees memory by removing unused objects.
Memory Manager: Manages memory allocation.
Exception Manager: Handles runtime exceptions.
Common Type System (CTS): Ensures compatibility between different
.NET languages.
Summary - ASP.NET 1
Common Language Specification (CLS): Unifies language rules
across .NET languages.
Intermediate Language (IL)
IL is partially compiled, CPU-independent code.
Compiled into native code by CLR before execution.
Technologies Supported by .NET
1. WinForms: For Windows desktop applications.
2. ASP.NET: For websites, web applications, and web services.
3. ADO.NET: For database interaction (e.g., SQL Server, XML).
4. LINQ: Query language integrated with C# and VB.NET.
Key Notes
BCL provides foundational libraries for development.
CLR ensures platform independence by converting code to system-specific
formats.
Diagram: .NET Framework Architecture
Component Purpose
BCL Pre-built classes for applications
CLR Runtime environment for executing .NET code
Languages Supported Over 60, including C#, VB.NET, and Python
Unit 2: ASP.NET Basics and Controls
Creating and Running Your First ASP.NET Application
Steps:
1. Create a New Project:
Open Visual Studio → Choose New → Project .
Select ASP.NET Web Application and specify the project name and
location.
2. Choose Project Type:
Select Web Forms (for basic applications).
3. Add a Web Form:
Right-click the project in Solution Explorer → Add → Web Form .
4. Write Code:
Summary - ASP.NET 2
Use the <% %> syntax for ASP.NET-specific code in the Web Form.
Example: To display "Hello World":
<%= Response.Write("Hello World") %>
Output:
After following these steps, you can run the project to see your application in
action.
ASP.NET Events and Event Handlers
Event: An action like a button click or key press.
Event Handler: A subroutine that specifies what happens when an event is
triggered.
Example: A button click in a web form:
protected void Button1_Click(object sender, EventArgs
e) {
Response.Write("Button clicked!");
}
ASP.NET Controls
Controls are used to create user interfaces. They allow users to interact with the
application.
Types of Controls:
1. HTML Controls:
Basic HTML elements made server-accessible using runat="server" .
Example:
<input id="UserName" type="text" runat="server" />
2. Server Controls:
Predefined ASP.NET controls with server-side functionality.
Syntax:
<asp:ControlType ID="ControlID" runat="server" Property
1="Value1" />
Example: A button control.
Summary - ASP.NET 3
<asp:Button ID="Button1" runat="server" Text="Submit" /
>
Difference Between HTML and Server Controls
Feature HTML Control Server Control
Execution Client-side Server-side
State Management Not supported Supported
Object-Oriented Not supported Supported
Speed Faster Slower (server processing)
Example <input> <asp:TextBox>
List Controls in ASP.NET
ASP.NET provides several list controls for user selections:
1. DropDownList:
Displays a dropdown with single-selection options.
Example:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:DropDownList>
2. ListBox:
Allows single or multiple selections.
Example:
<asp:ListBox ID="ListBox1" runat="server" SelectionMode
="Multiple">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:ListBox>
3. RadioButtonList:
Displays a group of radio buttons for single selection.
Example:
<asp:RadioButtonList ID="RadioButtonList1" runat="serve
r">
<asp:ListItem>Male</asp:ListItem>
Summary - ASP.NET 4
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
4. CheckBoxList:
Displays a group of checkboxes for multiple selections.
Example:
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:CheckBoxList>
5. BulletedList:
Displays items as an unordered or ordered list.
Example:
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:BulletedList>
Table Controls
Used to structure data into rows and columns.
Example:
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>Cell 1</asp:TableCell>
<asp:TableCell>Cell 2</asp:TableCell>
</asp:TableRow>
</asp:Table>
Web Control Events
Events in ASP.NET follow a sender-event pattern:
protected void EventName(object sender, EventArgs e) {
// Event-specific code
}
Key Notes
HTML Controls: Basic UI elements enhanced for server interaction.
Summary - ASP.NET 5
Server Controls: Rich features with built-in server-side functionality.
State Management: Essential for maintaining user data across postbacks.
Unit 3: ASP.NET Page Navigation, Master Pages, and
State Management
Page Navigation Options
Page navigation allows users to move between different web forms in an ASP.NET
application.
1. Response.Redirect
Redirects users to a new page (can be on the same server or an external
server).
Updates the browser’s history and address bar.
Syntax:
Response.Redirect("PageName.aspx");
Example:
Redirecting to another page:
Response.Redirect("HomePage.aspx");
2. Server.Transfer
Transfers users to another page on the same server without updating the
browser’s history or address bar.
Faster than Response.Redirect because it avoids an HTTP round trip.
Syntax:
Server.Transfer("PageName.aspx");
3. Cross-Page PostBack
Allows posting form data to another page.
Useful when you want to send data without using session or query strings.
Example:
Button1.PostBackUrl = "TargetPage.aspx";
4. Hyperlink Control
Navigates to another page using a clickable link.
Summary - ASP.NET 6
Example:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl
="TargetPage.aspx">Click Here</asp:HyperLink>
Master Pages
Master Pages define a consistent layout for a website. They act as templates for
web pages, ensuring a uniform design.
Features:
1. Consistent Layout:
Common elements like headers, footers, and navigation menus are shared
across pages.
2. Content Placeholders:
Define areas where child pages can insert specific content.
3. Separation of Design and Content:
Keeps layout separate from page-specific content for better
maintainability.
Creating and Using Master Pages
1. Create a Master Page:
Add a .master file to your project.
Define the layout using ContentPlaceHolder :
<asp:ContentPlaceHolder ID="MainContent" runat="serve
r"></asp:ContentPlaceHolder>
2. Create Content Pages:
Add a new .aspx file and link it to the Master Page.
Define page-specific content inside <asp:Content> tags:
<asp:Content ContentPlaceHolderID="MainContent" runat
="server">
<h2>Welcome to My Website</h2>
</asp:Content>
State Management
State management refers to techniques for preserving user data across page
requests.
Types of State Management:
Summary - ASP.NET 7
1. Client-Side State Management:
Stores data on the client machine.
Techniques:
ViewState:
Stores data for a single page.
Example:
ViewState["Key"] = "Value";
TextBox1.Text = ViewState["Key"].ToString();
Cookies:
Stores small data files on the client.
Example:
Response.Cookies["UserName"].Value = "John";
Hidden Fields:
Stores data in hidden HTML fields.
Example:
<input type="hidden" id="HiddenField1" value="Val
ue" runat="server" />
Query Strings:
Passes data in the URL.
Example:
Response.Redirect("TargetPage.aspx?Name=John");
2. Server-Side State Management:
Stores data on the server.
Techniques:
Session State:
Stores data per user session.
Example:
Session["UserName"] = "John";
Application State:
Stores data for the entire application.
Summary - ASP.NET 8
Example:
Application["AppName"] = "MyWebsite";
Static Members in Web Forms
Static members belong to the class, not instances of the class.
Useful for storing shared data across all users.
Example:
public static int Counter = 0;
Key Notes
Navigation Techniques:
Use Response.Redirect for external navigation.
Use Server.Transfer for faster internal navigation.
Master Pages:
Great for maintaining a consistent design across web pages.
State Management:
Choose client-side techniques for lightweight data storage.
Use server-side techniques for secure, persistent data.
Unit 4: ADO.NET and Data-Bound Controls
1. Introduction to ADO.NET
ADO.NET stands for ActiveX Data Objects for .NET.
A technology for interacting with data sources (e.g., relational databases,
XML).
Designed for building data-driven applications efficiently.
Key Components of ADO.NET
1. Connection:
Establishes a connection to a data source.
Example:
SqlConnection connection = new SqlConnection("connectio
nString");
connection.Open();
Summary - ASP.NET 9
2. Command:
Executes SQL queries or stored procedures.
Example:
SqlCommand command = new SqlCommand("SELECT * FROM User
s", connection);
3. DataReader:
Provides forward-only, read-only access to data.
Example:
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
Console.WriteLine(reader["ColumnName"]);
}
4. DataAdapter:
Fills a DataSet or DataTable and updates changes back to the database.
Example:
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * F
ROM Users", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
5. DataSet:
In-memory cache of data (supports multiple tables and relationships).
Example:
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Users");
2. ADO.NET Object Model
Key Classes and Their Roles:
Class Description
SqlConnection Connects to the database.
SqlCommand Executes SQL queries.
SqlDataReader Reads data row-by-row (forward-only).
SqlDataAdapter Populates and updates DataSet or DataTable .
DataSet In-memory representation of relational data.
Summary - ASP.NET 10
DataTable Represents a single table inside a DataSet .
DataView Filters and sorts data in a DataTable .
3. Workflow of ADO.NET
1. Open a connection to the database using SqlConnection .
2. Use SqlCommand or DataAdapter to execute SQL queries.
3. Retrieve data into a DataReader , DataTable , or DataSet .
4. Perform operations on data (e.g., filtering, sorting).
5. Update changes back to the database using DataAdapter .
4. Working with Data-Bound Controls
Data-bound controls simplify data display and interaction in ASP.NET.
Common Data-Bound Controls
1. GridView:
Displays data in a table with sorting, paging, and editing capabilities.
Example:
<asp:GridView ID="GridView1" runat="server" AutoGenerat
eColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Na
me" />
<asp:BoundField DataField="Email" HeaderText="E
mail" />
</Columns>
</asp:GridView>
2. Repeater:
Provides complete control over HTML rendering of data.
Example:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<p><%# Eval("Name") %></p>
</ItemTemplate>
</asp:Repeater>
3. DropDownList:
Displays a dropdown for selection.
Example:
Summary - ASP.NET 11
<asp:DropDownList ID="DropDownList1" runat="server" Dat
aTextField="Name" DataValueField="ID"></asp:DropDownLis
t>
4. DataList:
Displays data in a flexible list format.
Example:
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<div><%# Eval("Name") %></div>
</ItemTemplate>
</asp:DataList>
5. DetailsView:
Displays one record at a time in a table.
Example:
<asp:DetailsView ID="DetailsView1" runat="server"></as
p:DetailsView>
5. Populating Data into Data-Bound Controls
1. Configure the data source (e.g., SQL Database).
2. Bind the data to controls in the code-behind.
Example for GridView :
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
SqlConnection connection = new SqlConnection("c
onnectionString");
SqlDataAdapter adapter = new SqlDataAdapter("SE
LECT * FROM Users", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
}
6. Disconnected Data Access (Using DataSet)
Data is retrieved and stored in memory, allowing the database connection to
close while working with data.
Summary - ASP.NET 12
Example:
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Users");
7. Error Handling and Security in ADO.NET
1. Error Handling:
Use try-catch blocks to handle database exceptions.
Example:
try {
connection.Open();
} catch (SqlException ex) {
Console.WriteLine(ex.Message);
} finally {
connection.Close();
}
2. Security:
Use Parameterized Queries to prevent SQL Injection:
SqlCommand command = new SqlCommand("SELECT * FROM User
s WHERE ID = @ID", connection);
command.Parameters.AddWithValue("@ID", userInput);
Key Notes
Data-Bound Controls make it easy to present data from a database.
ADO.NET is designed for disconnected data access, improving performance.
Always prioritize security and proper error handling when working with
databases.
Summary - ASP.NET 13