CAT | Asp.Net
27
Dec:How will you apply view state at page level as well as at control level?
0 Comments | Posted by muskanpatil in Asp.Net
· Viewstate is one of the most important and useful client side state management mechanisms.
· It can store the page value at the time of post back of your page.
· ASP.NET pages provide the View State property as a built-in structure for automatically storing values between multiple requests for the same page.
· There are two level of view state:
1. Page level
2. Control level
· Page Level view state control treat as highest priorities.
· Which means If we set EnableViewState= “False” in page level, that will automatically be derived by all the server side controls. In that case, if we set EnableViewState=”True” for any server side control will treat it as false, as we have defined them “False” in Page Level.
· Apply the view state at page level:
o Page level means apply viewstate property to page.
o To apply the viewstate at page level set the property EnableViewState of page
o Now , if set the EnbleViewState property = “true” then all the server control derived this property as true.
o If we set the EnbleViewState property = “false” then all the server control derived this property as false.
· Apply the view state at control level:
o Control level means apply viewstate property on controls used in page.
o To apply the viewstate at control level then set the enableviewstate property of each control on which you want to set.
o Ex. <asp:DropDownList ID=”DropDownList1″ runat=”server” EnableViewState=”true”> </asp:DropDownList>
o If you apply page level as false then this property will not work because pagelevel has high priority.
A cookie is used to store small piece of information on client machine.A cookie contains page-specific information that a web server sends to a client along with page output.Cookies are used for sending page specific information because HTTP is a stateless protocol and cannot indicate whether page request coming from the same or different client.You can use cookies to keep track of individual user who access a web page across HTTP connection.
Cookies are saved on the client computer.Cookies can be either temporary or persistent.Temporary cookies also know as session cookies,exist in the memory space of browser.When the browser is closed,all the session cookies added to the browser are lost.A persistent cookie is saved as a text file in the file system of the client computer.
Cookies enable you to store information about a client,session,or application.When a browser request a page,it sends the information in the cookie along with the request information.A web server reads the cookie and extracts its value.
//Creating a cookie
HttpCookie sampleCookie = new HttpCookie(“UserColorSetting”);
sampleCookie.Values.Add(“Background”, txtBackgroundColor.Text);
sampleCookie.Expires = #12/31/2010#; Response.Cookies.Add(sampleCookie);
//Getting a cookie value from the user’s computer
String sGetCookie;
sGetCookie = Request.Cookies(“UserColorSetting”)(“Background”).ToString();
Advantages of Cookies:
1.A cookie is stored on the client computer and can be read by the server after a request for a page is posted.Therefore,no server resource is involved in maintaining the cookie.
2.A cookie is a text based data structure that contains key-value pairs.Therefore ,it is easy to create and manipulate cookie.
3.A cookie can either expire when the browser ends or exist indefinitely on the client computer,subject to the expiration rules on the client.
Limitation of Cookies:
1.Cookies that are stored on client computers have a limited size.Most browsers allow cookie to have up to 4096 bytes in size.Therefore you cannot store a large amount of data in a cookie.
2.Users can temper with cookies because cookies are stored on client computer.
3.Users can disable cookies to prevent from being stored on the hard disk of their computer.If a user denies permission for cookies,an ASP.net web application cannot store cookies on client computer.
User Control
you can create your own controls. You have two options. You can create:
- User controls. User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it.
- Custom controls. A custom control is a class that you write that derives from Control or WebControl.
User controls are substantially easier to create than custom controls, because you can reuse existing controls. They make it particularly easy to create controls with complex user interface elements.
User Control Structure
An ASP.NET Web user control is similar to a complete ASP.NET Web page (.aspx file), with both a user interface page and code. You create the user control in much the same way you create an ASP.NET page and then add the markup and child controls that you need. A user control can include code to manipulate its contents like a page can, including performing tasks such as data binding.
A user controls differs from an ASP.NET Web page in these ways:
- The file name extension for the user control is .ascx.
- Instead of an @ Page directive, the user control contains an @ Control directive that defines configuration and other properties.
- User controls cannot run as stand-alone files. Instead, you must add them to ASP.NET pages, as you would any control.
- The user control does not have html, body, or form elements in it. These elements must be in the hosting page.
To include a user control in a Web Forms page
1.In the containing ASP.NET Web page, create an @ Register directive that includes:
- A TagPrefix attribute, which associates a prefix with the user control. This prefix will be included in opening tag of the user control element.
- A TagName attribute, which associates a name with the user control. This name will be included in the opening tag of the user control element.
- A Src attribute, which defines the virtual path to the user control file that you are including.
2.In the body of the Web page, declare the user control element inside the form element.
3.Optionally, if the user control exposes public properties, set the properties declaratively.
Example
<%@ Page Language=”VB” %>
<%@ Register TagPrefix=”uc” TagName=”Spinner”
Src=”~/Controls/Spinner.ascx” %>
<html>
<body>
<form runat=”server”>
<uc:Spinner id=”Spinner1″
runat=”server”
MinValue=”1″
MaxValue=”10″ />
</form>
</body>
An application on the Web may consists of several ASP files that work together to perform some purpose. The Application object is used to tie these files together.
The Application object is used to store and access variables from any page, just like the Session object. The difference is that ALL users share ONE Application object (with Sessions there is ONE Session object for EACH user).
.NET provides a class called HttpApplication, which is part of the System.Web namespace. This class represents the Application object in ASP.NET. An instance of this class is already created for you and this is called “Application”.
The Application object holds information that will be used by many pages in the application (like database connection information). The information can be accessed from any page. The information can also be changed in one place, and the changes will automatically be reflected on all pages.
For example, if you assign a value into an Application variable as shown below, all users of the web site can access this value.
Example:
Application(“Current Stock Price”) = 145.25
The Application object’s collections, methods, and events are described below:
Collections
| Collection | Description |
| Contents | Contains all the items appended to the application through a script command |
| StaticObjects | Contains all the objects appended to the application with the HTML <object> tag |
Methods
| Method | Description |
| Contents.Remove | Deletes an item from the Contents collection |
| Contents.RemoveAll() | Deletes all items from the Contents collection |
| Lock | Prevents other users from modifying the variables in the Application object |
| Unlock | Enables other users to modify the variables in the Application object (after it has been locked using the Lock method) |
Events
| Event | Description |
| Application_OnEnd | Occurs when all user sessions are over, and the application ends |
| Application_OnStart | Occurs before the first new session is created (when the Application object is first referenced) |
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)
