Professional Documents
Culture Documents
Dot Net - Chapter-8
Dot Net - Chapter-8
Chapter-8
Why ASP.NET better than ASP (Or comparison ASP & ASP.NET)
3. If file name extension has not been mapped to asp.net engine, Asp.net will not
receive request. So IIS handles the request & discard the request without processing.
4. If IIS maps file Extension successfully, it load this ASPNET_ISAPI.DLL and pass request
to it.
5. Now ASPNET_ISAPI.DLL pass this request to ASPNET_WP.EXE Asp.net worker process
and now execution start. Then some page level events begins
1. Page PreInit
This is event fire before the Page Init.
- Check IsPostback property
- Set Master Page dynamically
- Set Theme property of page dynamically
- Recreate Dynamic controls
2. Init
In this method initialization done for all controls
3. Init Complete
It will fire if all initialization completed.
4. Pre Load
Use this event if you want to process the controls or code before the Page load
events.
5. Load
In this events page calls the OnLoad method of this page and recursively does the
same for each child controls until the page & all controls are loaded.
6. Control Events (Postback)
- This event handles specific control events such as Button control’s click event or
TextBox’s Textcahnged event. It is also called Postback.
- Each time you click a button, the page is sent back to the server and this process
repeats itself. The action of submitting a page back to the server is called a
Postback.
- At the beginning of every Postback, the Page. Load event fires, which you can
handle to initialize your page.
7. Pre Render
This is the Last event before the HTML Code generated for the page. Use this
events to make final changes.
8. Render
In this method page object call the render methods of each controls that writes
the controls markup and finally sent to the browser.
9. Unload
This event occur for each control and then for the page. Use this event to do final
clean up for specific control such as closing database connection etc..
6. Now if the page level events ends response with above events sends back to the IIS.
7. And Finally IIS sends the response to the client browser.
- Client side validation is good but we have to be dependent on browser and scripting
Language support.
- Client side validation is considered convenient for users as they get instant feedback.
The main advantage is that it prevents a page from being Postback to the server
until the client validation is executed successfully.
- For developer point of view serve side is preferable because it will not fail, it is not
Examples:
- The RequiredFieldValidator control ensures that the required field is not empty. If
you want any field compulsory then use this validation control.
</asp:RequiredFieldValidator>
- The RangeValidator control verifies that the input value falls within a predetermined
range.
- For example if you want to allow age only 18 to 40 years then Range validator used.
- It has three specific properties:
Properties Description
It defines the type of the data. The available values are: Currency,
Type
Date, Double, Integer, and String.
MinimumValue It specifies the minimum value of the range.
MaximumValue It specifies the maximum value of the range.
- The CompareValidator control compares a value of one control with a fixed value or
with a value of another control.
- For Password password and confirm password we can used this validation control.
- It has the following specific properties:
Properties Description
(4) RegularExpressionValidator
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="TextBox4" ErrorMessage="E-mail id not
valid" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
(5) CustomValidator
- The CustomValidator control allows writing specific custom validation for both the
client side and the server side validation.
- If your requirement is not fulfill with available inbuilt validation control then with
CustomValidator control you can write custom code and can use for validation.
- The client side validation is accomplished through the ClientValidationFunction
property. The client side validation routine should be written in a scripting
language, such as JavaScript or VBScript, which the browser can understand.
- The server side validation routine must be called from the control's ServerValidate
event handler. The server side validation routine should be written in any .Net
language, like C# or VB.Net.
(6) ValidationSummary
- The ValidationSummary control does not perform any validation but shows a
summary of all errors in the page. The summary displays the values of the
ErrorMessage property of all validation controls that failed validation.
1. Machine.config
2. Web.config
- Each and every asp.net application has its own configuration settings stored in
web.config file
- The configuration for each web application is unique
- If web application spam’s multiple folders ,each sub folders has its own web.config
file that inherit or overwrites or parents web.config file
- The main diff. between Machine.config & web.config is the file name
- Configuration files divided into multiple sections.
- The root element in xml configuration file is always <configuration>
- Note that the web.config file is case-sensitive, like all XML documents, and starts
every setting with a lowercase letter. This means you cannot write <AppSettings>
instead of <appSettings>.
You can update web.config settings at any point, even while your application is
running. If there are any requests currently under way, they’ll continue to use the
old settings, while new requests will get the changed settings right away.
with appropriate network rights, you can change a web.config file from a remote
computer. You can also copy the web.config file and use it to apply identical settings
to another application or another web server that runs the same application in a
web farm scenario.
The settings in the web.config file are human readable, which means they can be
edited and understood without needing a special configuration tool.
1. Connection String:
- The <connectionStrings> section allows you to define the connection information for
accessing a database.
- Connection string information stored either <appSettings/> or
<connectionStrings/> section.
- In asp.net 1.1 all the connection string info was stored in <app setting> section
- Examples , how to store connection string
<configuration>
<appSettings>
<add key="constr" value="datasource=sqlexpress; initial
catalog=test; Integrated security=true"/>
</appSettings>
</configuration>
SqlConnection con=New
SqlConnection(ConfigurationSettings.AppSettings["constr"])
- Using <ConnectionStrings>:
<configuration>
<connectionStrings>
<add name="constr" connectionString="datasource=sqlexpress;
initial catalog=test; Integrated security=true"/>
</connectionStrings>
</configuration>
2. Compilation Settings
- tempDirectory specifies the directory to use for temporary file storage during
compilation
- You can set compilation debug=true to insert debugging symbols into the compiled
page. And default is False
- Batch specifies whether batch compilation is support or not. And default is True
- maxBatchSize specifies the maximum no. of pages per batched compilation and
default value is 1000
- defaultLanguage specifies the default programming language such as VB or C# to use
in dynamic compilation files. And default is VB.
3. Page Settings
- With this setting we can set the general settings of a page like viewstate, MasterPageFile
and Themes.
</pages>
- By using the MasterPageFile and theme attributes, we can specify the master page
and theme for the pages in web application.
- Also we can enable or disable viewstate for particular page.
- In asp.net when error is occur then asp.net display the error page with source code and
line number of the error.
- Now if source code and error messages are displayed, it is possible to hack your site
code by hackers.
- So to overcome this problem asp.net provides excellent custom error mechanism.
- The <customErrors> section enables configuration of what to do if/when an unhandled
error occurs during the execution of a request. Specifically,it enables developers to
configure html error pages to be displayed in place of a error.
- In this example if error occurs, welcome.html page will be display instead of error.
5. Location Settings
- If you are working with a major project, you have numbers of folders and sub-folders, at
this kind of particular situation, you can have two options to work with.
- First thing is to have a Web.config file for each and every folder(s) and Sub-folder(s).
- And the second one is to have a single Web.config for your entire application.
- If you use the first approach, then you might be in a smoother way.
- But what if you have a single Web.config and you need to configure the sub-folder or
other folder of your application.
- The right solution is to use the "Location" tag of Web.config file.
<location path="Admin">
<system.web>
<pages theme="summer"></pages>
</system.web>
</location>
- As we all know, the ASP.NET is stateless and to maintain the state we need to use the
available state management techniques of ASP.NET.
- You can configure state management in web.config file with <sessionState> tag.
- For details see chapter-11 state management.
</sessionState>
7. Authentication Settings
<configuration>
<system.web>
<authentication mode="[Windows/Forms/Passport/None]">
</authentication>
</system.web>
</configuration>
- You can use authentication mode like windows, Forms, Passport and None
8. Authorization Settings
- <authorization> tag has list of access rules that either allow or deny a particular users.
- We can use <allow> or <deny> tag to create or modify access rules.
- Astrisk(*) means “All Users”
- Question Mark means “All Anonymous users”
- We can use <authorization> section like this:
<authorization>
<allow users="*"/>
<deny users="?"/>
<allow users="ark"/>
<allow roles="admin"/>
<deny roles="member"/>
</authorization>
Windows Authentication
- Windows based authentication is handled between the windows server where the
Asp.net application resides and the client machine.
- In windows based authentication, request goes directly to IIS to provide basic
authentication process.
- This type of authentication is quite useful in an intranet environment.
- To work with windows based authentication you have to create users & groups.
- You can assign windows authentication like,
<authentication mode="Windows"/>
<authorization>
<allow users ="*" />
</authorization>
Forms Authentication
- Forms authentication allows you to use custom login page with your own code. So end
user can simply enters his username & password into HTML form.
- This authentication mode is based on cookies where the user name and the password are stored
either in a text file or the database.
- After a user is authenticated, the user’s credentials are stored in a cookie for use in that session.
- When the user has not logged in and requests for a page that is insecure, he or she is redirected
to the login page of the application.
- Forms authentication supports both session and persistent cookies.
<configuration>
<system.web>
<authentication mode="Forms"/>
<forms name="login" loginUrl="login.aspx" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
- Here name is the name of cookie saved use to remember the user from request to
request.
- LoginUrl specifies the URL to which the request is redirected for login if no valid
authentication cookie is found.
Passport Authentication
9. httpRuntime settings
(1) AdRotator
<Ad>
<ImageUrl>Sunset.jpg</ImageUrl>
<NavigateUrl>http://www.kirc.ac.in </NavigateUrl>
<AlternateText>Sunset image</AlternateText>
<Impressions>1</Impressions>
<Keyword>Computer</Keyword>
</Ad>
</Advertisements>
- This example shows a single possible advertisement. To add more advertisements, you
can create multiple <Ad> elements and place them all inside the root <Advertisements>
element.
- After the creating XML file bind it with AdvertisementFile Property of AdRotator control.
Advertisement File Elements:
(2) Calendar
- The Calendar control presents a calendar that you can place in any web page.
- The Calendar control presents a single-month view, as shown in following Figure.
- The user can navigate from month to month using the navigational arrows.
- You can access a date which is selected by user at run time through code.
TextBox1.Text = Calendar1.SelectedDate.ToString();
End Sub
- With this code once you select a date that date should be display in TextBox1
- Calendar control has lots of property try it practically.
Note: Here I explain only Rich server controls. See all common control’s property and try
it practically. Also remember all the important property of each server control.