Section C
Section C
NET
ASP.NET Web Forms
• Web Forms are web pages built on the ASP.NET Technology.
• It executes on the server and generates output to the browser.
• It is compatible to any browser to any language supported by .NET common
language runtime.
• It is flexible and allows us to create and add custom controls.
• We can use Visual Studio to create ASP.NET Web Forms.
• It is an IDE (Integrated Development Environment) that allows us to drag and
drop server controls to the web forms. It also allows us to set properties,
events and methods for the controls.
• Web Forms are made up of two components: the visual portion (the ASPX
file), and the code behind the form, which resides in a separate class file.
Web forms code model
• The Web Forms model in ASP.NET consists of a server-side code file (. aspx
file) that contains a combination of HTML, server controls, and server-side
code that executes on the web server.
There are several benefits of Web Form Models of ASP.NET, including:
• Rapid development: Web Forms allow developers to quickly build complex
web applications with a drag-and-drop interface, a wide range of server
controls, and pre-built functionality.
• Easy data binding: Web Forms provide easy data binding, which simplifies the
process of binding data from various data sources to server controls on the
web page.
• Separation of concerns: Web Forms allow developers to separate the
presentation layer from the business logic layer, which makes the code easier
to maintain and update
• Event-driven programming: Web Forms use event-driven programming, which
simplifies the process of handling user input and server-side processing.
• Consistent user interface: Web Forms provide a consistent user interface
across all web pages using master pages, which help to create a cohesive look
and feel for the application.
• State management: Web Forms provide various mechanisms for state
management, such as view state, session state, and application state, which
simplify the process of maintaining the state of the web page and user input
data.
• Security: Web Forms provide built-in security features, such as authentication,
authorization, and encryption, which help to secure the application from
unauthorized access and malicious attacks.
• Compatibility: Web Forms are compatible with various browsers and devices,
which ensures that the application is accessible to a wide range of users.
• Traditional ASP pages are written with HTML elements, interspersed with
server side code in <% %> code blocks.
• The web page is contained in a single file.
• ASP.NET introduces a new programming model for Web Forms to
separate the code and visual elements in different files.
Two Models for Web Forms Pages
• Web Forms consist of two parts: visual elements and code logic.
• Web Forms pages support two models : Code-behind Web Forms and Single
File Web Forms.
• In the Single File Web Forms model, the code and the visual elements are
included in the same file.
• The code is contained in <script> blocks in the same file that contains the
visual elements.
• This is similar to the ASP programming model where the HTML elements and
<% %> tags containing server side code are contained in the same file.
• In the code-behind programming model, the visual elements are contained
in a .aspx file and the code is included in a separate class file.
• This results in a separation of code and UI resulting in web pages which are
easy to maintain.
Code Behind and Inline Code in ASP.NET
Code Behind
• Code Behind refers to the code for an ASP.NET Web page that is written in a
separate class file that can have the extension of .aspx.cs or .aspx.vb
depending on the language used.
• Here the code is compiled into a separate class from which the .aspx file
derives.
• You can write the code in a separate .cs or .vb code file for each .aspx page.
• One major point of Code-Behind is that the code for all the Web pages is
compiled into a DLL file that allows the web pages to be hosted free from any
Inline Server Code.
Inline Code
• Inline Code refers to the code that is written inside an ASP.NET Web Page
that has an extension of .aspx.
• It allows the code to be written along with the HTML source code using a
<Script> tag.
Step 1. First of all, create a new Blank Website in Visual Studio, then add a
Web page to it. Here we are first creating a Web page for the Code Behind so
remember one thing "The check box should be checked while adding this
page". In other words, check the "Place the code in a separate file" and then
click on the "Add" button.
Now on the .aspx page use a Button, a Link, and a Text Box.
• Step 2. Now double-click on the Button, this will use the Code window. Now
you will see that this coding section is opened in a separate window whose
extension is .aspx.cs. Write the code in this window. I wrote the code such
that whatever I wrote in the TextBox will also appear in the Label.
Now debug this page and verify that your program is running.
Step 3. Until now we were working on the Code Behind but now we will work
on the Inline Code, for that add another web page to your Web Site. But this
time things are different, this time don't check the check box and if it's
checked then Uncheck it and then click on "Add".
Now on this new .aspx page again use a Button, a Link, and a Text Box.
ASP.NET - Life Cycle
ASP.NET Application Life Cycle
ASP.NET Page Life Cycle
• When a page is requested, it is loaded into the server memory,
processed, and sent to the browser.
• Then it is unloaded from the memory.
ASP.NET Page Life Cycle Events
• At each stage of the page life cycle, the page raises some events,
which could be coded.
• An event handler is basically a function or subroutine, bound to the
event, using declarative attributes such as Onclick or handle.
ASP.NET - Event Handling
• An event is an action or occurrence such as a mouse click, a key press,
mouse movements, or any system-generated notification.
• A process communicates through events.
• For example, interrupts are system-generated events.
• Events in ASP.NET raised at the client machine, and handled at the server
machine.
• For example, a user clicks a button displayed in the browser. A Click event
is raised. The browser handles this client-side event by posting it to the
server..
• The server has a subroutine describing what to do when the event is
raised; it is called the event-handler.
Event Arguments
• ASP.NET event handlers generally take two parameters and return void.
• The first parameter represents the object raising the event and the
second parameter is event argument.
Application and Session Events
Page and Control Events
Event Handling Using Controls
• All ASP.NET controls are implemented as classes, and they have events
which are fired when a user performs a certain action on them.
• For example, when a user clicks a button the 'Click' event is generated.
• For handling events, there are in-built attributes and event handlers.
ASP.NET - Server Controls
• Controls are small building blocks of the graphical user interface, which
include text boxes, buttons, check boxes, list boxes, labels, and numerous
other tools.
• Controls are also used for structural jobs, like validation, data access,
security, creating master pages, and data manipulation.