Asp. NET has two important objects, one is the application object and the other is the session object.
Application: An object that records application parameters that are used to share application-level information.
Session: A variable object that records the browser side and is used to store variables or objects that are cross-Web program programs.
To tell the truth, the Asp.net,application object that has been written for almost a year is really not used. Read a book, according to the characteristics of these two objects wrote a simple chat room program. It's really very shabby.
My idea is that there are two pages of the default page and chatroom page, page layout
Default page:
Chatroom page:
The idea is that in the default page login, the nickname entered into the Session object, and jump to the chatroom page, and the session in the key to "nickname" stored in the Application object. Iterate through the Application object and add the nickname of the online person to the listbox on the left. Use the Appliaction object to +1,-1 the key to "count" in the Session_Start and Session_End methods to record the current number of people online. The chat record is stored with the Application object key for "content". The code below
Global.asax:
protected voidApplication_Start (Objectsender, EventArgs e) {application["Count"] = 0; application["Content"] ="Chat history \ n"; }protected voidSession_Start (Objectsender, EventArgs e) {application.lock (); application["Count"] = (int) application["Count"] + 1; Application.UnLock (); }protected voidSession_End (Objectsender, EventArgs e) {application.lock (); application["Count"] = (int) application["Count"]-1; Application.UnLock (); }
Default.aspx:
protected void Button_login_click ( Span class= "KWRD" >object sender, EventArgs e) {if (!string . IsNullOrEmpty (Textbox_nickname. Text) {session[ "nickname" ] = textbox_nickname. Text; } Response.Redirect ( "chatroom.aspx" ); }
Chatroom.aspx:
protected voidPage_Load (Objectsender, EventArgs e) {label_count. Text ="Current number of people online"+ application["Count"]. ToString () +"People";if(! IsPostBack) {if(session["nickname"] !=NULL) {Application.add (session["nickname"]. ToString (), session["nickname"]); } textbox_records. Text = application["Content"]. ToString (); } listbox_usernames. Items.clear ();foreach(stringStrinchApplication.Contents) {if(!str. Equals ("Content") &&!str. Equals ("Count") &&!str. Equals ("Name") {Listbox_usernames. Items.Add (NewListItem (Application[str]. ToString (), Application[str]. ToString ())); } } }protected voidButton_send_click (Objectsender, EventArgs e) {if(session["nickname"] !=NULL) {application["Content"] + = (session["nickname"]. ToString () +"say:"+ textbox_sendmsg. Text +"\ n"); Textbox_records. Text = application["Content"]. ToString (); } }protected voidButton_exit_click (Objectsender, EventArgs e) {application.remove (session["nickname"]. ToString ()); application["Count"] = (int) application["Count"]-1; Response.Redirect ("Webcounter.aspx"); }
This simple chat room is finished, when used locally, in a browser does not seem to create a new session, you need to login in different browsers to see the effect.
It is also important to note that when exiting the chat room, the application key is "nickname", that is, the value of the current session["nickname") is removed. Otherwise, when you refresh the listbox, you will find that the nickname for the exit is still present.
Also the key to "count" of the Application Object-1, perhaps you have to ask, is not in the Session_End method already-1? I'm really not sure, maybe there are two reasons, do not know which one? The friend who wants to understand gives advice.
1, the session did not expire, I just put application in the corresponding key for the session of the object killed, but the real session I did not clear. Therefore, the Session_End method is not called. (but perhaps you will ask, you put the current session["nickname") object empty is OK? I tried this, but count didn't-1. )
2, will it be because I am in the local two different browser to do this chat operation reason?
In some special cases, using the Application object to our help is really very large, the session I have to say, definitely we often use.