Nothing Special   »   [go: up one dir, main page]

Concepts and Examples: Aniket Sarkar Sanjib Sarkar

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

ASP.

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.

• The markup would be in one file (for Eg. SamplePage.aspx)

• The class is declared with the partial keyword

• 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

single file separate files

code

<tags> <tags> code

Form1.asp Form1.aspx Form1.cs


Code behind
Library classes
ASP.NET Server Controls (1 of 2)

 Server controls are tags that are understood by the server.

There are three kinds of server controls:


• HTML Server Controls - Traditional HTML tags [E.g. Simple Text tags for display]
• Web Server Controls - New ASP.NET tags
[Fully Programmable Objects]
• Validation Server Controls - For input validation

• SHOW EXAMPLES
ASP.NET Server Controls (2 of 2)

 Server controls encapsulate behavior


 Declarative, tag with runat="server“

 Generate HTML that is sent to the client


 Can support multiple client types
 DHTML, HTML 3.2, WML, etc.

 Process input sent from client


 Bind to data in Forms collection
 Fire events

 Allow look and feel to be customized

 Are stateless on the server


Stateless:
• It does not keep track of configuration settings, transaction information or any
other data for the next session.
• It cannot take information about the last session into the next, such as settings
the user chose or conditions that arose during processing.
• The HTTP protocol, which is the communications vehicle for Web transactions, is
stateless.
• After a Web page is delivered to the user, the connection is closed.

OR it can be simple web definition:


A protocol is stateless if there is no relation between subsequent request-
response pairs. The server can handle each request uniquely and does not
have to keep a session state for the client.

State management has to be coded manually.


ASP.NET Events (1 of 2)
 ASP.NET Web forms use event-based model.
 Every server control can have one or more event/s.
 Example: a button control can have an event handler to handle the click event.
 Event is a way for a class to provide the notifications to the clients of the class when something
interesting happens to an object.
 it’s like giving the instruction for an action.

Application/Session Life Cycle Events:


Protected void Application_Start(object sender, EventArgs e)

Events for the Page Life Cycle:


Protected void Page_Load(Object sender,EventArgs e)

Events for the Server Controls:


Protected void ButtonID_Click(Object sender,EventArgs e)

Each function has three things in common:


 Object Argument - It represents the object that raised the event.
 Event Args - Event object contains any event specific information.
 Name of Event - All these follow a naming convention.
ASP.NET Events (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

All ASP.Net controls have various properties / attributes.


Properties can be readonly or read-write.
The controls can be manipulated through 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

 Stored in XML file in directory with pages


 web.config
 Contains all ASP.NET settings
 Authentication, connection strings, compile options, custom error pages, etc.
 Allows adding Application settings
 DSN, etc.
 Extensible
 Can extend with custom configuration data
Configuration File Features – Web.Config

 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)

 Added to Web Form like any other server control


 Controls validation for a variety of requirements
 Required field
 Range checking
 Pattern matching
 You can attach more than one validation control to an input
 Only works with a subset of controls
Controls You Can Validate (2 of 3)

Control Validation Property


HtmlInputText Value
HtmlTextArea Value
HtmlSelect Value
HtmlInputFile Value
TextBox Text
ListBox SelectedItem.Value
DropDownList SelectedItem.Value
RadioButtonList SelectedItem.Value
Types of Validation (3 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

 All validation controls are checked


 If all pass muster the Page’s IsValid property is set to True

 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

You might also like