Professional Documents
Culture Documents
NET Training
Table of Contents
1 2 3 What is ASP.Net? ........................................................................................................ 2 ASP.Net Web Forms Model: ....................................................................................... 2 ASP.NET - Life Cycle ................................................................................................. 2 3.1 3.2 3.3 4 5 When the Page is requested for the first time....................................................... 3 Life cycle when the page processed during a postback event .............................. 3 Different Events during the Asp.NET page Life Cycle ....................................... 4
Page directives: ............................................................................................................ 4 ASP.NET - Event Handling......................................................................................... 4 5.1 5.2 5.3 5.4 What is an Event?................................................................................................. 4 Application and Session Events: .......................................................................... 4 Page and Control Events: ..................................................................................... 5 Default Events: ..................................................................................................... 5 Control Types ....................................................................................................... 6 ASP.Net server controls ....................................................................................... 6 UserControls......................................................................................................... 6 ASP.NET Validation Controls ............................................................................. 7
RequiredFieldValidator .....................................................................................7 CompareValidator ............................................................................................7 RangeValidator.................................................................................................7 RegularExpressionValidator...............................................................................7 CustomValidator...............................................................................................7
OutputCache ......................................................................................................... 7
OutputCache - VarybyParam .............................................................................8 OutputCache - varybycontrol.............................................................................8 OutputCache - varybycustom ............................................................................8 OutputCache - varybyheader.............................................................................8 OutputCache - Substitution ...............................................................................8
7.1.1
7.2
Santosh .NET Training 8 9 10 11 12 12.1 Cookies ........................................................................................................................ 8 Sessions........................................................................................................................ 9 9.1 Session State Modes............................................................................................. 9 ViewState ................................................................................................................. 9 Web.config ............................................................................................................. 10 Authentication and Authorization in ASP.NET..................................................... 10 Authentication .................................................................................................... 10
Authentication in ASP.NET ..............................................................................10 Authentication in IIS .......................................................................................11 12.1.1 12.1.2
13 13.1 13.2
1 What is ASP.Net?
1. 2. 3. 4. ASP.Net is a part of Micros oft .Net Framework. ASP.Net works on top of the HTTP protocol and Uses the HTTP commands and policies t o set a brows er-t o-server t wo-way communication and cooperation. ASP.Net applications are complied codes, written using the extensible and reusable components or objects present in .Net framework. These codes can us e the entire hierarchy of classes in .Net framework. ASP.Net is used to produce interactive, data-driven web applications over the internet.
5.
Page 2
3.2 Life cycle when the page processed during a postback event
The processing sequence in which a page is proc essed during a postback event is: Initializing: During this phase, the server creates an instance of the server cont rol Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control. Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined. Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client. PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering. Saving state: During this phas e, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised. Rendering: During this phase, the server creates the corres ponding HTML tag for the control.
Page 3
4 Page directives:
The page directives set up the environments for the page to run. The @Pa ge directive defines page-specific attribut es used by the ASP.Net page parser and compiler. Page directives specify how the page should be processed, and which assumptions are to be taken about the page. It allows importing names paces, loading assemblies and registering new controls with custom tag names and namespace prefixes. We will discuss all of these concepts in due time.
Page 4
Page 5
6 ASP.NET Controls
Cont rols are small building blocks of the graphical user interface, which includes text boxes, buttons, check boxes, list boxes, labels and numerous other tools, using which users can enter data, make selections and indic ate their preferences. Cont rols are also used for structural jobs, like validation, data access, security, creating master pages, data manipulation.
6.3 UserControls
A UserControl is a separate, reusable part of a page. You can put a piece of a page in a UserControl, and then reus e it from a different location. You need to use a Register directive on the page to make use of this usercontorl in that page.
<%@ Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/UserInfoBoxControl.ascx" %>
OR You can add it to the controls element in the web.config file in order to us e the userc ontrol without specifying the Register Directive in all the pages.
Page 6
You can add a usercontrol either on the markup or dy namically to the control collection of the page or placeholder.
6.4.1 RequiredFieldValidator
You can use it to make sure that the user has entered something in a TextBox control. You can set enableclientscript="false" for it validate only on the server side. just check the Page.IsValid on the server side (in C# code in your code behind file) to make sure the data is validated.
6.4.2 CompareValidator
It can compare two values, for instance the values of two controls.
6.4.3 RangeValidator
It makes sure that the user input is within a specified range. You can use it to validate both numbers, strings and dates, which can make it useful in a bunch of cases.
6.4.4 RegularExpressionValidator
RegularExpressionValidator is one of the most useful validators, because it can be used to check the validity of any kind of string, wit h the help of Regular Expressions.
6.4.5 CustomValidator
If none of the other validators can help you, the CustomValidator usually can. It doesn't come with a predefined way of working; you write the code for validating your self. This is of course very powerful, since the possibilities are basically endless. <asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom" onservervalidate="cusCustom_ServerValidate" errormessage="The text must be exactly 8 characters long!" />
7 Caching
Caching in ASP.NE T refers to the strategy of keeping a copy of a page or image or set of objects in the memory of the web server.
7.1 OutputCache
Output Cache directive, is by far the easiest way of caching cont ent with ASP.NE T. As you will see in our example, it doesnt even require any code only some minor changes to the markup of the page, and youre good to go <%@ Output Cache duration="10" varybyparam="None" %>
Page 7
8 Cookies
Cookies are small pieces of text, stored on the client's computer to be used only by the website setting the cookies. This allows webapplications to save information for the user, and then re-use it on each page if needed.
Page 8
9 Sessions
Sessions can be used to store even complex data on the server side. Sessions data will be per user basis. At any time, a user can access only his data. Sessions make use of cookie to keep track of who the user is. Session Id will be stored in the cookie. If you want to do a cookieless session state, then it will store the sessionid in the uri. Session Data can be stored either of the following modes. o None No session will be used in the application. o In Proc This is how session state is handled by default. The advantage of using session state in process is that it's very fast and convenient. However, it's not durable. For example, if you restart IIS or somehow knock the server down, all session state is lost. You cannot use it in a Web Farm. o StateServer Advant age of this approach is that the session management is
done on a separate process running on a particular machine. This option also gives you the advantage of running your server in a Web farm. The downside to this approac h is that it does impede performance s omewhat -applications need to make a network round-trip to the state server when loading or saving session information. SQL Server Stores session information within a SQL Server database somewhere on your net work. Use this option when y ou want to run your server from within a Web farm when you want session state to be durable and safe.
10 ViewState
The ViewState allows ASP.NET to repopulate form fields on each postback to the server, making sure that a form is not automatically cleared when the user hits the submit button.
Page 9
The Viewstate on the page and the controls are enabled by default. You can use ViewState (Which is of type Stat eBag ) to store your own values also. ViewState is limited to page. It cannot carry values between pages. ViewState is pretty good for storing simple values for use in the form, but if you wish to save more complex data, and keep t hem from page t o page, you s hould look into using cookies or sessions, as described in the previous chapters.
11 Web.config
Web.config acts as a central location for storing the information to be accessed by web pages . This information could be a Connection String stored at a centralized location so that it can be accessed in a data-driven page. If the connection string changes its just a matter of changing it at one place. All parent Web.config file settings will be inherited by child Web.config files. All child Web.config files can overwrite the settings listed in their parent Web.config files unless you specify the setting to not be overwritten. The Web.config file in your current working directory will have the most dominate settings. All settings are inside of <configuration> xml tags. The Web.config file is an xml file which must be declared at the top of the document. The <system.web> tag contains elements that more server specific such as security, custom error pages, etc. The <appSettings> tag holds more of the information that can be used on actual applications such as sql connection strings.
Page 10
12.1.2.1 Anonymous
IIS doesn't perform any authentication. Any one is allowed to access the ASP.NET application.
12.1.2.2 Basic
Users must provide a windows userna me and password to connect. This information is sent over the network in clear text, Very much insecure over the internet.
12.1.2.3 Digest
Users must still provide a windows user name and password to connect. The password is hashed before it is sent across the network. Digest authentication requires that all users be running Int ernet Explorer 5 or later and that windows accounts to stored in active directory.
Page 11
13 Master Pages
13.1 Uses of Master Pages
The master pages can be used to accomplish the following: Creating a set of controls that are common across all the web pages and attaching them to all the web pages. A centralized way to change the above created set of controls which will effectively change all the web pages. Dynamically changing the common UI elements on master page from content pages based on user preferences.
13.2 Terminology
Let us look at the basic terminology that needs to be understood before jumping into master pages: Masterpage: Gives us a way to create common set of UI elements that are required on multiple pages of our website. Cont entPage: The ASP.NE T web page that will use master page to have the common UI elements displayed on rendering itself. Cont entPlaceHolder: A control that should be added on the MasterPage which will reserve the area for the content pages to render their contents. Cont entControl: A control which will be added on content pages to tell these pages that the contents inside this control will be rendered where the MasterPage's ContentPlaceHolder is located.
Page 12