Concepts and Examples: Aniket Sarkar Sanjib Sarkar
Concepts and Examples: Aniket Sarkar Sanjib Sarkar
Net
Concepts and Examples
Aniket Sarkar
Sanjib Sarkar
ASP.NET Web Page Contents
• Visual elements, which include markup, server controls, and static text.
• Programming logic for the page, which includes event handlers and other code.
ASP.NET Architecture
ASP.Net Site
Web Forms – traditional drag and drop server controls, server events and state management
techniques.
MVC – new design technique which allow fast and agile development.
Web Pages - has built-in template and helpers. It is best for developing beautiful web application with
latest web standards.
SPA - Single Page Application which helps to build web applications that include significant client-side
interactions using HTML5, CSS3 and JavaScript. It is best to make highly interactive single page
dashboard web applications.
Asp.NET Services
Web API - framework for building HTTP services that can be consume by a broad range of clients
SignalR - library that simplifies the process of adding real-time web functionality to applications. Real-
time web functionality is the ability to have server code push content to connected clients instantly as
it becomes available, rather than having the server wait for a client to request new data.
The Code-Behind Page Model
• The code-behind page model allows you to keep the markup in one file—the .aspx
file—and the programming code in another file.
• When the page runs, the compiler assembles them into a single class
• Advantages
– Clean separation of the markup (user interface) and code.
– Code is not exposed to page designers
– Code can be reused for multiple pages
– SHOW EXAMPLES
Separation of Code, Content
ASP page combines declarative tags (HTML, ASP directives, server controls and static text)
with code
ASP.NET separates the code and tags for better manageability
Advantages:
Compiled code
strong typing
early binding
Structured error handling
code
• SHOW EXAMPLES
ASP.NET Server Controls (2 of 2)
Page Load
The Page object calls the OnLoad method on the Page object, and then recursively does
the same for each child control until the page and all controls are loaded.
The Load event of individual controls occurs after the Load event of the page.
This is the first place in the page lifecycle that all values are restored.
Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
Use the OnLoad event method to set properties in controls and establish database
connections.
Page Load is invoked each time there is a post back due to some other event. Example:
button click.
PostBack
Process of submitting the ASP.NET page back to the server (simply a subsequent request to
a page).
It says to the server that the client is posting data back to you for the processing.
Some controls have AutoPostBack properties, while some do not.
//EXAMPLES
ASP.NET Life Cycle Events (1 of 3)
ASP.NET Life Cycle Events (2 of 3)
Event Use
PreInit Check the IsPostBack property to determine whether this is the first time the page is being processed.
Set a master page dynamically.
Set the Theme property dynamically.
Init Raised after all controls have been initialized and any skin settings have been applied.
The Init event of individual controls occurs before the Init event of the page.
InitComplete Raised at the end of the page's initialization stage. ‘View state’ tracking enables controls to persist any values that are programmatically
added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks.
PreLoad Raised after the page loads view state for itself and all controls, and after it processes postback data.
Load The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and
all controls are loaded. Used to set properties in controls and to establish database connections.
LoadComplete Use this event for tasks that require that all other controls on the page be loaded.
PreRender Raised after the Page object has created all controls that are required in order to render the page. Use the event to make final changes to
the contents of the page or its controls before the rendering stage begins.
PreRenderComplete Raised after each data bound control whose DataSourceID property is set calls its DataBind method.
SaveStateComplete Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this
point affect rendering, but the changes will not be retrieved on the next postback.
Render All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser. If you create a
custom control, you typically override this method to output the control's markup.
Unload Raised for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-
specific database connections. page itself, use this event to do final cleanup work, such as closing open files and database connections, or
finishing up logging or other request-specific tasks.
ASP.NET Life Cycle Events (3 of 3)
ASP.NET Control Properties
TextBox Properties
• Rendering properties like BackColor, BorderColor, BorderStyle, Visible
• Enabled
• Text
• TextMode
• ClientID (ReadOnly)
• ViewState (ReadOnly)
DropDownList Properties
• Rendering properties like BackColor, BorderColor, BorderStyle, Visible.
• DataSource
• Enabled
• SelectedIndex
• SelectedValue (ReadOnly)
Configuration
A Web application can contain more than one Web.config file. The settings in a file apply to
the directory in which it's located, and all child directories. Web.config files in child directories
take precedence over the settings that are specified in parent directories.
Web.config files are protected by IIS, so clients cannot get to them. If you try to retrieve an
existing http://mydomain.com/Web.config file, you'll be presented with an "Access denied"
error message.
IIS monitors the Web.config files for changes and caches the contents for performance
reasons. There's no need to restart the Web server after you modify a Web.config file.
Validation Controls in ASP.NET (1 of 3)
Required Field
Input MUST be entered
Compare
Input must match a pre-defined value or other control value
Range
Input must be within a lower- and upper-range
Regular Expression
Input must conform to a regular expression pattern
Custom
Uses a custom function you create
Summary
Summarizes the results of all the validation controls
Validation happens always on the server, once the page is submitted.
Displaying Validation Errors
When input fails validation, the associated validation control’s Text property is displayed
If the Text property is empty, it displays the ErrorMessage property
A summary validation control displays the ErrorMessage properties for all the Validation
controls on the page
This is only displayed when the page’s IsValid property is False
Data Binding
ASP.NET Deployment
“XCOPY” deployment
No registration required for components
No stopping the server for updates
Supports all web resources
Web pages, web services
Compiled components (DLL)
Configuration files
Update running apps
Simply copy DLL on top of old ones
App gracefully migrated to new code