You are on page 1of 12

Santosh .

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

ASP.NET Controls ................................................................................................... 6 6.1 6.2 6.3 6.4


6.4.1 6.4.2 6.4.3 6.4.4 6.4.5

Caching ........................................................................................................................ 7 7.1


7.1.2 7.1.3 7.1.4 7.1.5

OutputCache ......................................................................................................... 7
OutputCache - VarybyParam .............................................................................8 OutputCache - varybycontrol.............................................................................8 OutputCache - varybycustom ............................................................................8 OutputCache - varybyheader.............................................................................8 OutputCache - Substitution ...............................................................................8

7.1.1

7.2

Caching objects .................................................................................................... 8


Page 1

Santosh ASP.NET Interview Preparation

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

Master Pages .......................................................................................................... 12 Uses of Master Pages ......................................................................................... 12 Terminology ....................................................................................................... 12

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.

2 ASP.Net Web Forms Model:


ASP.Net web forms extend the event -driven model of interaction to the web applications. The browser submits a web form to the web server and the server returns a full markup page or HTML page in response. All client side user activities are forwarded to the server for stateful processing. The server processes the output of the client actions and triggers the reactions.

3 ASP.NET - Life Cycle


ASP.Net life cycle specifies, how: ASP.Net processes pages to produce dynamic out put

Santosh ASP.NET Interview Preparation

Page 2

Santosh .NET Training


The application and its pages are instantiated and processed ASP.Net compiles the pages dynamically

3.1 When the Page is requested for the first time


The Life Cycle of a page when requested for the first time: Initializing: During this phase, the server creates an instance of the server cont rol Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined. PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering. Saving: During this phase, the state information of the control is saved. For example, if a value is set for the control during the Load event, it is embedded in the HTML tag that will be returned to the browser. Rendering: During this phase, the server creates the corres ponding HTML tag for the control. Di sposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed. Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server cont rol

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.

Santosh ASP.NET Interview Preparation

Page 3

Santosh .NET Training


Di sposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed. Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server cont rol

3.3 Different Events during the Asp.NET page Life Cycle


The events associated with the relevant page cycle phases are: Page Initialization: Page_Init View State Loading:LoadViewState Postback data processing: LoadPostData Page Loading: Page_Load PostBack Change Notification: RaisePostDataChangedE vent PostBack Event Handling: RaisePostBackE vent Page Pre Rendering Phase: Page_P reRender View State Saving: SaveViewState Page Rendering: Page_Render Page Unloading: Page_UnLoad

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.

5 ASP.NET - Event Handling


5.1 What is an Event?
E vent is an action or occurrence like mouse click, key press, mouse movements, or any system generated notific ation. The proc esses communicate through events. When events occur the application should be able to respond to it.

5.2 Application and Session Events:


The most important application events are: Application_Start . it is raised when the application/website is started Application_End . it is raised when the application/website is stopped Similarly, the most used Session events are: Session_Start . it is raised when a user first requests a page from the application

Santosh ASP.NET Interview Preparation

Page 4

Santosh .NET Training


Session_End . it is raised when the session ends

5.3 Page and Control Events:


Common page and control events are: DataBinding . raised when a control bind to a data source Di sposed . when the page or the control is released Error . it is a page event, occurs when an unhandled exception is thrown Init . raised when the page or the control is initialized Load . raised when the page or a cont rol is loaded PreRender . raised when the page or the control is to be rendered Unload . raised when the page or control is unloaded from memory

5.4 Default Events:


The default event for the Page object is the Load event. Similarly every control has a default event. For example, default event for the button control is the Click event. The default event handler c ould be created in Visual Studio, just by double clicking the control in design view. The following table shows some of the default events for common cont rols: Control AdRotat or BulletedList Button Calender CheckBox CheckBoxList DataGrid DataList DropDownList HyperLink ImageButton ImageMap LinkButton ListBox Menu RadioButton RadioButtonList AdCreated Click Click SelectionChanged CheckedChanged SelectedIndexChanged SelectedIndexChanged SelectedIndexChanged SelectedIndexChanged Click Click Click Click SelectedIndexChanged MenuItemClick CheckedChanged SelectedIndexChanged Default Event

Santosh ASP.NET Interview Preparation

Page 5

Santosh .NET Training

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.1 Control Types


ASP.Net uses five types of web controls, which are: HTML controls HTML Server controls ASP.Net Server cont rols ASP.Net Ajax Server controls User cont rols and custom controls

6.2 ASP.Net server controls


ASP.Net server controls are the primary controls used in ASP.Net. These c ontrols agai n could be grouped into the following categories: Validation control s - these are used to validate user input and work by running client side script Data source control s - these cont rols provides data binding to different dat a sources Data view controls - these are various lists and tables, which can bind to data from data sources for display Personalization control s - these are used for personaliz ation of a page according to the user's preference, based on user information Login and security control s - these controls provide user aut hentication Master pages - these provides consistent layout and interface throughout the applic ation Navigation control s - these helps in navigation, for example, the menus, tree view etc. Rich control s - these implements special features, for example, AdRotator control, FileUpload control, Calendar control etc.

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.

Santosh ASP.NET Interview Preparation

Page 6

Santosh .NET Training

You can add a usercontrol either on the markup or dy namically to the control collection of the page or placeholder.

6.4 ASP.NET Validation Controls


The validation controls of ASP.NE T is based on both clientside and serverside technology, .

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" %>

Santosh ASP.NET Interview Preparation

Page 7

Santosh .NET Training 7.1.1 OutputCache - VarybyParam


The varybyparam is a required parameter of the OutputCache directive. It specifies a list of parameters which the cache should be varied by.

7.1.2 OutputCache - varybycontrol


The varybycont rol does exactly what it says - it varies the cache depending on the value of the specified control. <%@ Output Cache duration="10" varybyparam="none" vary bycontrol="NameOfControl" %>

7.1.3 OutputCache - varybycustom


It allows you to handle the variations yourself, by setting a custom string, which ASP.NET will vary the output by. To test this, you need to add a global.asax file to your project, if you don't already have one. In the Global. asax file, you need to override the GetVaryByCustomString method. <%@ Output Cache duration="120" varybyparam="None" varybycustom="Browser" %>

7.1.4 OutputCache - varybyheader


This one allows you to vary the content based on one or more of the headers that browser send. Here is an example: <%@ Output Cache duration="120" varybyparam="None" varybyheader="Accept-Language" %> This will vary cache based on the Accept-Language header, whic h tells the server which languages the user accepts and prefers.

7.1.5 OutputCache - Substitution


The Substitution tag takes an attribute called met hodname, which holds the name of a met hod which should return the string to output. Once the page is loaded, no matter if it's returned from the cache or freshly generated, this method will be called, and the Substitution cont rol will be filled with the returned string.

7.2 Caching objects


You will use System.Web.Cachin.Cac he class object to add the object that need to be cached on the server. You will make use of Add method of the Cache class to add whatever the object or collection of objects, you need to be cached. Cache.Add("datestamps", datestamps, null, System.Web.Caching. Cache.NoA bsoluteExpiration, new TimeSpan(0, 0, 60), System.Web.Caching. CacheItemP riority.Default, null);

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.

Santosh ASP.NET Interview Preparation

Page 8

Santosh .NET Training


Cookies are domain limited. They can be accessed by any user using the site on the same browser on the same machine. No sensitive information should ever be stored in a cookie.

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.

9.1 Session State Modes


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.

Santosh ASP.NET Interview Preparation

Page 9

Santosh .NET Training

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.

12 Authentication and Authorization in ASP.NET


12.1 Authentication
Authentication is the process of identifying who the user is. It can be done in two levels o ASP.NET application o IIS

12.1.1 Authentication in ASP.NET


ASP.net gives you a choice of three different authentication providers. Below are the three.

12.1.1.1 The windows Authentication


This provider lets you authenticate users based on their windows accounts. This provider uses IIS to perform the authentication and then passes the authenticated identity to your code. This is the default provided for ASP.net. <authentication mode ="windows">

Santosh ASP.NET Interview Preparation

Page 10

Santosh .NET Training 12.1.1.2 The forms authentication


This provider uses custom HTML forms to collect authentication information and lets you use your own logic to authenticate users. The user's credentials are stored in a cookie for use during the session. <authentication mode ="forms">

12.1.1.3 The passport authentication


The provider uses Microsoft's passport service to authenticate users. <authentication mode ="passport ">

12.1.2 Authentication in IIS


IIS gives you a choice for four different authentication methods:

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.

12.1.2.4 Windows Integrated


Passwords never cross the network. Users must still have a username and password Application uses either the Kerberos or challenge/response protocols authenticate the user. Windows -integrated aut hentication requires that all users be running internet explorer 3.01 or later It is designed to provide strong authentication for client/server applications by using secret-key cryptography.

Santosh ASP.NET Interview Preparation

Page 11

Santosh .NET Training

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.

Santosh ASP.NET Interview Preparation

Page 12

You might also like