Download - Santosh - ASP.net Interview Preparation
-
7/29/2019 Santosh - ASP.net Interview Preparation
1/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 1
Table of Contents1 What is ASP.Net? ........................................................................................................ 2
2 ASP.Net Web Forms Model: ....................................................................................... 2
3 ASP.NET - Life Cycle ................................................................................................. 2
3.1 When the Page is requested for the first time....................................................... 3
3.2 Life cycle when the page processed during a pos tback event .............................. 3
3.3 Different Events during the Asp.NET page Life Cycle ....................................... 4
4 Page directives: ............................................................................................................ 4
5 ASP.NET - Event Handling......................................................................................... 4
5.1 What is an Event?................................................................................................. 4
5.2 Application and Session Events: .......................................................................... 45.3 Page and Control Events: ..................................................................................... 5
5.4 Default Events: ..................................................................................................... 5
6 ASP.NETControls ................................................................................................... 6
6.1 Control Types ....................................................................................................... 6
6.2 ASP.Net server controls ....................................................................................... 6
6.3 UserControls......................................................................................................... 6
6.4 ASP.NET Validation Controls ............................................................................. 7
6.4.1 RequiredFieldValidator ........................................ .............................................7
6.4.2 CompareValidator ........................................ ................................................. ...7
6.4.3 RangeValidator.................................................................................................7
6.4.4 RegularExpressionValidator...............................................................................7
6.4.5 CustomValidator...............................................................................................7
7 Caching ........................................................................................................................ 7
7.1 OutputCache ......................................................................................................... 7
7.1.1 OutputCache - VarybyParam .............................. ...................................... .........8
7.1.2 OutputCache - varybycontrol................................................................ .............8
7.1.3 OutputCache - varybycustom ....................................... .....................................8
7.1.4 OutputCache - varybyheader........................................ .....................................8
7.1.5 OutputCache - Substitution .......................................... .....................................8
7.2 Caching objects .................................................................................................... 8
-
7/29/2019 Santosh - ASP.net Interview Preparation
2/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 2
8 Cookies ........................................................................................................................ 8
9 Sessions........................................................................................................................ 9
9.1 Session State Modes............................................................................................. 9
10 ViewState ................................................................................................................. 9
11 Web.config ............................................................................................................. 10
12 Authentication and Authorization in ASP.NET..................................................... 10
12.1 Authentication .................................................................................................... 10
12.1.1 Authentication in ASP.NET ............................................. .................................10
12.1.2 Authentication in IIS .............................................. .........................................11
13 Master Pages .......................................................................................................... 12
13.1 Uses of Master Pages ......................................................................................... 12
13.2 Terminology ....................................................................................................... 12
1 What is ASP.Net?
1. ASP.Net is a part of Microsoft .Net Framework.
2. ASP.Net works on top of the HTTP protocol and
3. Uses the HTTP commands and policies to set a browser-to-server two-waycommunication and cooperation.
4. ASP.Net applications are complied codes, written using the extensible and reusablecomponents or objects present in .Net framework. These codes can use the entirehierarchy of classes in .Net framework.
5. ASP.Net is used to produce interactive, data-driven web applications over the internet.
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 HTMLpage in response.
All client side user acti vities are forwarded to the server for state ful processing. The serverprocesses 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 output
-
7/29/2019 Santosh - ASP.net Interview Preparation
3/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 3
The application and its pages are instantiated and processedASP.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 control
Loading: During this phase, the instance of the control is loaded onto the page object in which itis defined.
PreRendering: During this phase, the control is updated with the changes made to it. Thisprepares the control for rendering.
Saving: During this phase, the state information of the control is saved. For example, if a value isset for the control during the Load event, it is embedded in the HTML tag that will be returned tothe browser.
Rendering: During this phase, the server creates the corresponding HTML tag for the control.
Disposing:During this phase, all cleanup tasks, such as closing files and database connectionsopened by the control are performed.
Unloading: During this phase, all c leanup tasks, such as destroying the instances of servercontrol are performed. This is the final event in the life cycle of a server control
3.2 Life cycle when the page processed during a postback eventThe processing sequence in which a page is processed during a postback event is:
Initializing: During this phase, the server creates an instance of the server control
Loading view state: During this phase, the view state of the control posted by the client isreloaded into the new instance of the control.
Loading: During this phase, the instance of the control is loaded onto the page object in which itis defined.
Loading the postback data: During this phase, the server searches any data corresponding tothe 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 phase, the change in the state of control between the current requestand the previous request of the page is saved. For each change, the corresponding event israised. For example, if the text of a textbox is changed, the new text is saved and a text_changeevent is raised.
Rendering: During this phase, the server creates the corresponding HTML tag for the control.
-
7/29/2019 Santosh - ASP.net Interview Preparation
4/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 4
Disposing: During this phase, all cleanup tasks, such as closing files and database connectionsopened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of servercontrol are performed. This is the final event in the life cycle of a server control
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: RaisePostDataChangedEvent PostBack Event Handling: RaisePostBackEvent
Page Pre Rendering Phase: Page_PreRender 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 @Page directive defines
page-specific attributes used by the ASP.Net page parser and compiler. Page directives specifyhow the page should be processed, and which assumptions are to be taken about the page.
It allows importing namespaces, loading assemblies and registering new controls with custom tagnames and namespace prefixes. We will discuss all of these concepts in due time.
5 ASP.NET - Event Handling
5.1 What is an Event?
Event is an action or occurrence like mouse click, key press, mouse movements, or any system
generated notification. The processes communicate through events. When events occur theapplication should be able to respond to it.
5.2 Application and Sess ion Events:
The most important application events are:
Application_Start . it is raised when the application/website is startedApplication_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
-
7/29/2019 Santosh - ASP.net Interview Preparation
5/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 5
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
Disposed . 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 control is loaded
PreRender. raised when the page or the control is to be renderedUnload . 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 defaultevent. For example, default event for the button control is the Click event.
The default event handler could be created in Visual Studio, just by double clicking the control indesign view. The following table shows some of the default events for common controls:
Control Default Event
AdRotator AdCreated
BulletedList Click
Button Click
Calender SelectionChanged
CheckBox CheckedChanged
CheckBoxList SelectedIndexChanged
DataGrid SelectedIndexChanged
DataList SelectedIndexChanged
DropDownList SelectedIndexChanged
HyperLink Click
ImageButton Click
ImageMap Click
LinkButton Click
ListBox SelectedIndexChanged
Menu MenuItemClick
RadioButton CheckedChanged
RadioButtonList SelectedIndexChanged
-
7/29/2019 Santosh - ASP.net Interview Preparation
6/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 6
6 ASP.NET Controls
Controls 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 enterdata, make selections and indicate their preferences.
Controls are also used for structural jobs, like validation, data access, security, creating masterpages, data manipulation.
6.1 Control Types
ASP.Net uses five types of web controls, which a re:
HTML controls
HTML Server controls
ASP.Net Server controls
ASP.Net Ajax Server controlsUser controls and custom controls
6.2 ASP.Net server controls
ASP.Net server controls a re the primary controls used in ASP.Net. These controls agai n could begrouped into the following categories:
Validation controls - these are used to validate user input and work by running client-side script
Data source controls - these controls provides data binding to different data sources
Data view controls - these are various lists and tables, which can bind to data from data
sources for display
Personalization controls - these are used for personalization of a page according to the
user's preference, based on user information
Login and security controls - these controls provide user authentication
Master pages - these provides consistent layout and interface throughout the application
Navigation controls - these helps in navigation, for example, the menus, tree view etc.
Rich controls - 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 reuse it from a different location.
You need to use a Register directive on the page to make use of this usercontorl in that page.
OR
You can add it to the controls element in the web.config file in order to use the usercontrol withoutspecifying the Register Directive in all the pages.
-
7/29/2019 Santosh - ASP.net Interview Preparation
7/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 7
You can add a usercontrol either on the markup or dynamically to the control collection of the
page or placeholder.
6.4 ASP.NET Validation ControlsThe validation controls of ASP.NET 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) tomake sure the data is validated.
6.4.2 CompareValidatorIt can compare two values, for instance the values of two controls.
6.4.3 RangeValidatorIt 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 RegularExpressionValidatorRegularExpressionValidator is one of the most useful validators, because it can be used to checkthe validity of any kind of string, with the help of Regular Expressions.
6.4.5 CustomValidatorIf none of the other validators can help you, the CustomValidatorusually 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.
7 Caching
Caching in ASP.NET 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 OutputCacheOutputCache directive, is by far the easiest way of caching content with ASP.NET. As you willsee in our example, it doesnt even require any code only some minor changes to the markup ofthe page, and youre good to go
-
7/29/2019 Santosh - ASP.net Interview Preparation
8/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 8
7.1.1 OutputCache - VarybyParam
The varybyparam is a required parameter of the OutputCache directive. It specifies a list ofparameters which the cache should be varied by.
7.1.2 OutputCache - varybycontrol
The varybycontrol does exactly what it says - it varies the cache depending on the value of thespecified control.
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'talready have one. In the Global.asax file, you need to override the GetVaryByCustomStringmethod.
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:
This will vary cache based on the Accept-Language header, which tells the server which
languages the user accepts and prefers.
7.1.5 OutputCache - Substitution
The Substitution tag takes an attribute called methodname, which holds the name of a methodwhich should return the string to output. Once the page is loaded, no matter if it's returned fromthe cache or freshly generated, this method will be called, and the Substitution control will befilled with the returned string.
7.2 Caching objects
You will use System.Web.Cachin.Cache class object to add the object that need to be cached onthe 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.NoAbsoluteExpiration, new TimeSpan(0, 0, 60),System.Web.Caching.CacheItemPriority.Default, null);
8 CookiesCookies are small pieces of text, stored on the client's computer to be used only by thewebsite setting the cookies. This allows webapplications to save information for the user, andthen re-use it on each page if needed.
-
7/29/2019 Santosh - ASP.net Interview Preparation
9/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 9
Cookies are domain limited.
They can be accessed by any user using the site on the same browser on the samemachine.
No sensitive information should ever be stored in a cookie.
9 SessionsSessions 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 bestored in the cookie. If you want to do a cookieless session state, then it will store thesessionid 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 ofusing 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 serverdown, all session state is lost. You cannot use it in a Web Farm.
o StateServer Advantage of this approach is that the session management isdone on a separate process running on a particular machine. This option alsogives you the advantage of running your server in a Web farm. The downside to
this approach is that it does impede performance somewhat -applications need tomake a network round-trip to the state server when loading or saving sessioninformation.
o SQL Server Stores session information within a SQL Server databasesomewhere on your network. Use this option when you want to run your serverfrom within a Web farm when you want session state to be durable and safe.
9.1 Session State ModesSession 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 ofusing 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 serverdown, all session state is lost. You cannot use it in a Web Farm.
o StateServer Advantage of this approach is that the session management isdone on a separate process running on a particular machine. This option alsogives you the advantage of running your server in a Web farm. The downside to
this approach is that it does impede performance somewhat -applications need tomake a network round-trip to the state server when loading or saving sessioninformation.
o SQL Server Stores session information within a SQL Server databasesomewhere on your network. Use this option when you want to run your serverfrom within a Web farm when you want session state to be durable and safe.
10ViewState
The ViewState allows ASP.NET to repopulate form fields on each postback to the server, makingsure that a form is not automatically cleared when the user hits the submit button.
-
7/29/2019 Santosh - ASP.net Interview Preparation
10/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 10
The Viewstate on the page and the controls are enabled by default.
You can use ViewState (Which is of type StateBag) 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 them from page to page, you should look into using cookies or sessions,as described in the previous chapters.
11Web.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 beaccessed in a data-driven page. If the connection string changes its just a matter of changing it atone 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.configfiles unless you specify the setting to not be overwritten.
The Web.config file in your current working directory will have the most dominatesettings.
All settings are inside of xml tags.
The Web.config file is an xml file which must be declared at the top of the document.
The tag contains elements that more server specific such as security,custom error pages, etc.
The tag holds more of the information that can be used on actualapplications such as sql connection strings.
12Authentication and Authorization in ASP.NET
12.1Authentication
Authentication is the process of identifying who the user is.
It can be done in two levelso ASP.NET application
o IIS
12.1.1Authentication in ASP.NET
ASP.net gives you a choice of three different authentication providers. Below are the three.
12.1.1.1The windows AuthenticationThis 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 thedefault provided for ASP.net.
-
7/29/2019 Santosh - ASP.net Interview Preparation
11/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 11
12.1.1.2The forms authenticationThis provider uses custom HTML forms to collect authent ication information and lets you use yourown logic to authenticate users. The user's credentials are stored in a cookie for use during thesession.
12.1.1.3The passport authenticationThe provider uses Microsoft's passport service to authenticate users.
12.1.2Authentication in IISIIS gives you a choice for four different authentication methods:
12.1.2.1Anonymous
IIS doesn't perform any authentication.
Any one is allowed to access the ASP.NET application.
12.1.2.2Basic
Users must provide a windows username and password to connect.
This information is sent over the network in clear text,
Very much insecure over the internet.
12.1.2.3DigestUsers 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 Internet Explorer 5 or later andthat windows accounts to stored in active directory.
12.1.2.4Windows IntegratedPasswords never cross the network.
Users must still have a username and password
Application uses either the Kerberos or challenge/response protocols authenticate theuser.
Windows-integrated authentication requires that all users be running internet explorer3.01 or later
It is designed to provide strong authentication for client/server applications by usingsecret-key cryptography.
-
7/29/2019 Santosh - ASP.net Interview Preparation
12/12
Santosh .NET Training
Santosh ASP.NET Interview Preparation Page 12
13Master Pages
13.1Uses 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 themto all the web pages.
A centralized way to change the above created set o f controls which will e ffectivelychange all the web pages.
Dynamically changing the common UI elements on master page from content pagesbased on user preferences.
13.2Terminology
Let us look at the basic terminology that needs to be understood before jumping into masterpages:
Masterpage: Gives us a way to create common set of UI elements that are required onmultiple pages of our website.
ContentPage: The ASP.NET web page that will use master page to have the common UI
elements displayed on rendering itself.
ContentPlaceHolder: A control that should be added on the MasterPage which willreserve the area for the content pages to render their contents.
ContentControl: A control which will be added on content pages to tell these pages thatthe contents inside this control will be rendered wherethe MasterPage's ContentPlaceHolderis located.