The Session object is a dictionary of name-value pairs. You can associate any Common Language Runtime (CLR)-based object with a key of your choosing and place it in the Session object so it will be there when the next request belonging to that session comes through. Then you may access that piece of data using the key under which it was stored. For example, if you wanted to store some information provided by the user in the Session object, you’d write code like this:
void StoreInfoInSession() { String strFromUser = TextBox1.Text; Session["strFromUser"] = strFromUser; } To retrieve the string during the next request, you’d use code like this: void GetInfoFromSession() { String strFromUser = Session["strFromUser"] ; // NOTE: may be null TextBox1.Text = strFromUser; }
The square braces on the Session object indicate an indexer. The indexer is a convenient syntax for expressing keys—both when inserting data into and retrieving data from the Session object. Do note, however, that if the key you provide doesn’t map to a piece of data in the session dictionary, the Session object will return null. In production code it’s always wise to check for a null value and react accordingly.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|