UNITE- 4
1) Introducing ASP.NET Web Forms
ASP.NET Web Forms is a framework that allows developers to build dynamic web
applications using a rich set of controls and components. It is part of the broader ASP.NET
framework and is primarily focused on event-driven development, similar to the way desktop
applications are created with Windows Forms.
2) Creating Web Forms Application Projects in ASP.NET
ASP.NET Web Forms is an ideal platform for building dynamic, data-driven web
applications with a familiar drag-and-drop interface for UI components. Creating a Web
Forms project involves several steps, from setting up the environment to building the
application using server-side controls and managing the application’s lifecycle.
Steps to Create Web Forms Application Projects
1. Setting Up the Development Environment: To create an ASP.NET Web Forms
project, you need to install the appropriate tools:
o Visual Studio: This is the recommended IDE for developing ASP.NET Web
Forms applications. It provides templates for Web Forms and integrated tools
for testing, debugging, and publishing.
Creating a New Web Forms Project: In Visual Studio, creating a Web Forms project is
straightforward:
• Step 1: Open Visual Studio and select File > New > Project.
• Step 2: Choose the ASP.NET Web Application (.NET Framework) template.
• Step 3: In the next dialog, choose Web Forms as the type of ASP.NET project you
want to create.
• Step 4: Give the project a name and select the folder location. Visual Studio will set up
the necessary folders and files, including the default Web Forms pages.
Understanding the Folder Structure: When a new Web Forms project is created, Visual
Studio generates several folders and files:
• App_Data: Used to store application data such as database files or XML.
• App_Code: Contains reusable classes or components for the application.
• App_Themes: Holds theme files (CSS, images, etc.) to control the application's look
and feel.
• Global.asax: Used for application-wide event handling like application start, session
start, or error handling.
Designing the User Interface (UI): Web Forms applications use a drag-and-drop interface for
designing UI:
• Using Server Controls: Visual Studio provides a toolbox with several server controls
like Button, TextBox, DropDownList, and GridView. You can drag and drop these
controls onto the .aspx page to create the UI.
• Master Pages: Master pages help create consistent layouts across the application. You
can create a master page (e.g., Site.Master) and then attach content pages to it. This
enables the application to have a consistent header, footer, and navigation across all
pages.
• HTML and CSS: You can also manually add HTML elements and apply custom CSS
to create a more customized and responsive design.
Using Web Controls in ASP.NET Web Forms:
Web controls in ASP.NET Web Forms are server-side controls that allow developers to create
dynamic, interactive web applications. These controls simplify the process of building web-
based user interfaces by abstracting common UI components, such as text boxes, buttons,
labels, and more.
Key Features of Web Controls:
1. Server-Side Processing: Web controls run on the server, which means they generate
HTML and handle user inputs on the server side.
2. State Management: ASP.NET Web Forms provides built-in mechanisms to maintain
the state of web controls between postbacks using view state.
3. Event Handling: Each control can trigger events (like button clicks) that are handled
in server-side code.
4. Rich Set of Controls: ASP.NET provides a wide variety of built-in controls such as
TextBox, Button, Label, DropDownList, GridView, CheckBox, RadioButton, and
more.
5. Validation Controls: ASP.NET offers controls like RequiredFieldValidator and
RangeValidator to perform input validation on both client and server sides.
3) ASP.NET Web Controls:
• TextBox: Used for accepting user input.
• Button: Used to submit forms or trigger actions.
• Label: Displays static or dynamic text.
• DropDownList: Allows users to select an option from a list.
• GridView: Displays data in a tabular format with options for sorting, paging, and
editing.
Example:
asp
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
In this example, the TextBox is used to collect user input, the Button triggers an event on the
server, and the Label is used to display a message to the user.
4) Working with Events in ASP.NET Web Forms:
In ASP.NET Web Forms, events allow interaction between the user and the server.
Events are actions that occur when users interact with web controls, such as clicking a
button, selecting an item from a dropdown, or changing text in a textbox. ASP.NET
provides a robust event-handling mechanism that allows developers to write code that
responds to these user actions.
Postback:
• In Web Forms, many events trigger a postback, which means the page is submitted to
the server for processing, and the server generates a new response.
• For example, when a button is clicked, a postback occurs, and the server handles the
associated event.
Event Model:
• Events in ASP.NET follow a delegate-based model, where event handlers are
methods that respond to events triggered by controls.
Event Handlers:
• Event handlers are methods in your code-behind file that execute when a specific
event occurs. These methods must match the event delegate signature.
• Common events include Click, TextChanged, SelectedIndexChanged, and
CheckedChanged.
AutoPostBack:
• Controls like DropDownList and TextBox can trigger postbacks automatically when
their values change if their AutoPostBack property is set to True.
Button Click Event:
The Click event is raised when a button is clicked by the user.
asp
Copy code
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />