Showing posts sorted by relevance for query session. Sort by date Show all posts
Showing posts sorted by relevance for query session. Sort by date Show all posts

ASP.Net Session

Session
HTTP is stateless protocol which means that server treats each request independently, session enables the user to save and retrieve values while user navigates the web app.
The collection of session variables is indexed by the name of the variable or by an integer index. Session variables are created by referring to the session variable by name. You do not have to declare a session variable or explicitly add it to the collection. The following example shows how to create session variables in an ASP.NET page for the first and last name of a user, and set them to values retrieved from TextBox controls.
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
Session variables can be any valid .NET Framework type. The following example stores an ArrayList object in a session variable named StockPicks. The value returned by the StockPicks session variable must be cast to the appropriate type when you retrieve it from the SessionStateItemCollection.
// When retrieving an object from session state, cast it to
// the appropriate type.
ArrayList stockPicks = (ArrayList)Session["StockPicks"];
// Write the modified stock picks list back to session state.
Session["StockPicks"] = stockPicks;
Sessions are identified by a unique identifier that can be read by using the SessionID property. When session state is enabled for an ASP.NET application, each request for a page in the application is examined for a SessionID value sent from the browser. If no SessionID value is supplied, ASP.NET starts a new session and the SessionID value for that session is sent to the browser with the response.
By default, SessionID values are stored in a cookie. However, you can also configure the application to store SessionID values in the URL for a "cookieless" session.
A session is considered active as long as requests continue to be made with the same SessionID value. If the time between requests for a particular session exceeds the specified time-out value in minutes, the session is considered expired. Requests made with an expired SessionID value result in a new session.
The text above is taken from Microsoft.
Read More
Save Values in Session State
This example uses the HttpSessionState object to persist values within an individual session.
string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;
The text above is taken from Microsoft.
Read More
Read Values from Session State
This example accesses the Item property to retrieve the values in session state.
string firstName = (string)(Session["First"]);
string lastName = (string)(Session["Last"]);
string city = (string)(Session["City"]);
The text above is taken from Microsoft.
Read More

State management is an art which retains user specific info between requests but most of the time the scope of info is global to entire app, To achieve the goal of state management in the app you may mix up the following server side state management options  
For further question please mail: brainstormiert@gmail.com




ASP.Net Application State

HTTP protocol is a stateless protocol which does not help to maintain the state of the page for example when a request is sent to server by client an instance of the page is created and when the server respondes the instance and the value of control(s) is destroyed, there many techniques to maintain the state of the page one of them is Application state
Application state provides info based on application scope, this info is visible and could be used throughout the application. The info is stored in application state and this info is maintained by the server.
An example is when multiple sessions share the data which does not change is the best type of data to use through application state. It is very easy to use because only one copy of the info is good enough to share between multiple pages.
But on the hand it is highly recommended not to store unique values in application state because this will be global to only particular process and each process may have different unique values.

State management is an art which retains user specific info between requests but most of the time the scope of info is global to entire app, To achieve the goal of state management in the app you may mix up the following server-side state management options

For further question please mail: brainstormiert@gmail.com

ASP.Net View state

ASP.Net helps maintain state info in different ways by keeping it on the page or on the local machine. It reduces the usage of server resources because the info is kept on the local machine, because no information is maintained on server side between trips.

Viewstate is one of the Client-Based State Management Options


In Viewstate a dictionary object is used to keep the values between multiple requests of the page. Viewstate is the default method to preserve page between round trips. The current state of page and its controls is kept(hashed) in a string in the page in hidden field or in the multiple hidden fields if the value is more than the MaxPageStateFieldLength of the hidden field.

The page parses the string at page initialization and restores the page info after the page is posted back to the server.

Viewstate is part of state management because we is stateless, it means every time a postback occurs a new instance of page class is created.
For example if we enter a text into a textbox a refresh the page the textbox will be empty because  the page is recreated.
Viewstate allows the text to repapulate after each postback, which makes sure that form is not cleared after the ok button is clicked. Viewstate can't arry values between pages but keeps it
unto page only.(see the image below)
Now if you run the project and enter your name in textbox and press ok button the name will be saved in viewstate and also assigned to label, if you press the refresh button it does nothing but postback to the server or refreshes the page.
After you press the refresh button you will notice the label still shows the name but textbox is empty, if you press the ok button again with textbox empty you will notice that label still shows the name its because of viewstate.
Viewstate is good unto page lavel but if you wish to transfer data between pages session or cookie might be used.
State management is an art which retains user specific info between requests but most of the time the scope of info is global to entire app, To achieve the goal of state management in the app you may mix up the following client side state management options  
For further question please mail: brainstormiert@gmail.com

PostBack

Postback

Postback is the process when an ASP page is submitted to the server. It is done if some data is to be checked against database or when some data is to reloaded or refreshed after inserting or updating a record. It is a round trip from client side to the server and bacck to client.

Postback is originated from client side browser for example when user presses the save button. Then button will initiate the postback.

For example if save button is clicked

if (IsPostBack)
{
   InsertRecord();
}

IsPostback propert determines if page is posted back from server or it is loading for the first time.

If loading for the first time a gridview could be binded as following in the page load event.
if(!IsPostback)
{
   BindData();
}

During the postback ASP uses viewstate to maintain the state of controls on the form. It is a method which preserves the state of control between round trips. It is done on the page level and is turned on by default. It keeps the value of control without using a session.


AutoPostback



Controls which can not submit the form on their own are provided a property know as Autopostback.
For example if different data is to be shown on dropdownlist selected index change event, if it's Autopostback property is set to true it will fetch records based on its value, which will result in data shown. If control's Autopostback property is set to true .Net framwork inserts some code in HTML to implement this functionality.
For example
1  Java script method with name __doPostBack (eventtarget, eventargument)
2 Two Hidden variables with name __EVENTTARGET and __EVENTARGUMENT
3 OnChange JavaScript event to the control






For further question please mail: brainstormiert@gmail.com

ASP.Net Profile Propety

A website when provides the user to customise the look and feel according to their preferences with their personal touch, it is much liked.
Using profile properties is the best way to provide this functionality along with an important aspect of recognising the user and their preferences and present them with their custom layout.

To implement user profiles we need to follow the following steps
1. Specify where the user profile will be saved. this is done by specifying the Profile Provider.
2. After configuring the profile provider, we have to decide what information we need to save for the user for example  define the user profile.
3. After this we establish a mechanism to identify the user in a unique way, because not all of the visitors of the website will be members.
4. After indentifying the users uniquely we will notice his preferences and store them in profile provider.
The default settings of profile only authenticates the user who are memebers and regularly visiting the website but we have to configure it for anonymous users or guest visitors.
The profile provider specifies the store where the user preferences are stored. Typically this is database. This needs to be done in the web.config file.
Following is an example
<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>
The configuration shows the default profile provider which is local database placed in the App_Data directory of the website. If we need to specify the custom profile provider then we need to specify the connectionString intead of connectionStringName to use the desired database.
Also, we will be needing to set the database to accept and save the profile related data. this is done by running the aspnet_regsql command against the database which will to be used to save the profile data.
After configuring the profile provider, we have to specify the user data that we need to save. We do this by having the properties in the provider element of web.config.
Following is an example
<profile>
<properties>
<add name="Name" allowAnonymous="true"/>
<add name="VisitedOn" type="System.DateTime" allowAnonymous="true"/>
</properties>
</profile>
The allowAnonymous="true" specifies that the anonymous users will be able to save their profile information. To work this properly we need to specify <anonymousIdentification enabled="true"/> in the web.config file.
After completing this successfully we are able to save all properties we need to track as the user profile information and it works for authenticated as well as anonymous users.
Following is an example of accessing the profile property
string name = Profile.Name;         
DateTime lastVisited = Profile.VisitedOn;
Following is an example of setting the profile property
Profile.Name = TextBox1.Text;
Profile.VisitedOn = DateTime.Now;
Profile.Save();
Profile functionality differs from cokkies in a way that if the user deletes the cookies then the info stored in cookie will be lost.

For further question please mail: brainstormiert@gmail.com