The Application object is used to store and access variables from any page, similar to the Session object. The difference is that all users share a Application object, and the session object and the user's relationship is one by one corresponding. The Application object holds information (such as database connection information) that is used by many pages in the application. This means that the information can be accessed from any page. It also means that you can change this information in one place, and then these changes are automatically reflected on all pages.
The creation and acquisition of application is the same as the session:
application["KeyName"]= ...; or Application.add ("KeyName", value). The assigned content can be any type of data. The value obtained by application["KeyName" is an object and needs to show the type of conversion.
Since application is in the entire program and is shared by all users, it needs to be locked and unlocked in order to avoid another user being modified at the time of the modification.
Application.Lock ();
application["KeyName"]= ....
Application.UnLock ();
Similarly, the removal of application also requires a lock and unlocking. Application.remove ("KeyName") clears an item, Application.clear () clears all.
This feature of application can be used to record the number of visits to a website.
protected voidButton5_click (Objectsender, EventArgs e) { if(application["App_count"] !=NULL) {application.lock (); application["App_count"] = (int) application["App_count"] +1; Application.UnLock (); } Else{application.lock (); application["App_count"] =1; Application.UnLock (); } } protected voidButton7_click (Objectsender, EventArgs e) { if(application["App_count"] !=NULL) Button7.text="Click to view current count:"+ application["App_count"]. ToString (); ElseButton7.text="no count of application"; } protected voidButton6_click (Objectsender, EventArgs e) {Application.Lock (); Application.remove ("App_count"); Application.UnLock (); }
After running, in the browser with two pages to open this page, respectively, after viewing the click button, on the other page to see the current count changes.
A simple chat room
The first is the design landing page:
Login button:
protected void Button1_Click (object sender, EventArgs e)
{
session["UID"] = TextBox1.Text;
Server.Transfer ("default2.aspx");
}
Then design the chat page:
<meta http-equiv="refresh" content="5"/><!-- This is used for automatic page refresh, refreshed every 5 seconds, added to Head tab--
<form id="Form1"runat="Server"> <div> <asp:panel id="Panel1"runat="Server"Bordercolor="#FF9933"borderstyle="Double"height="369px"scrollbars="Vertical"Width="536px"></asp:Panel> <asp:panel id="Panel2"runat="Server"> <asp:textbox id="TextBox1"runat="Server"></asp:TextBox> <asp:button id="Button1"runat="Server"text="Send"onclick="Button1_Click"/> <asp:button id="Button2"runat="Server"text="Quit Chat"onclick="button2_click"/> </asp:Panel> </div> </form>
Public Partial classdefault2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { if(session["UID"] ==NULL)//This session is for each user to have one aloneResponse.Redirect ("Default.aspx"); if(application["Chat"] ==NULL)//application is for all users to share aapplication["Chat"] =NewPanel (); PANEL1.CONTROLS.ADD (Panel) application["Chat"]); } protected voidButton2_Click (Objectsender, EventArgs e) {//Quit ChatSession.remove ("UID"); Response.Redirect ("Default.aspx"); } protected voidButton1_Click (Objectsender, EventArgs e) {//SendLabel uid =NewLabel (); Uid. Text= session["UID"]. ToString (); Label DT=NewLabel (); Dt. Text= DateTime.Now.ToLongTimeString () +":"; Label Word=NewLabel (); Word. Text=TextBox1.Text; Literal LBR=NewLiteral (); Lbr. Text="<br/>"; Application.Lock (); (Panel) application["Chat"]). Controls.addat (0, LBR); (Panel) application["Chat"]). Controls.addat (0, Word); (Panel) application["Chat"]). Controls.addat (0, DT); (Panel) application["Chat"]). Controls.addat (0, UID); Application.UnLock (); }}
Run: Log in to Zhang San and John Doe at the login interface,
Then separate into the respective chat interface
This is John Doe.
This is Zhang San.
Each time you refresh, the contents of the text box in front of the Send button will be erased. This is because this refresh is a refresh of the entire page, not a partial refresh. This refresh there is another place to note, that is, after the first user login, if not the first time to send information, then after 5 seconds will automatically jump back to the login page, it appears that if (session["uid"] = = null) The result of this judgment is true, this has not been understood.
To implement a partial refresh, Ajax,jquery and IFRAME are available.
Using an IFRAME:
The first is to create a new page DEFAULT3
"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title> <meta http-equiv="Refresh"Content="5"/>"Form1"runat="Server"> <div runat="Server"Id="Holder"> </div> </form></body>
Background code:
protected void Page_Load (object sender, EventArgs e) { if (application["chat " NULL ) application["chat"new Panel (); Holder. Controls.Add (panel) application["chat"); }
For the previous Chat page Default2 make the following changes:
First remove the auto-refresh tag:
The panel that displays the chat record is then removed and the IFRAME is added:
DEFAULT2 code behind the scenes
protected voidPage_Load (Objectsender, EventArgs e) { if(session["UID"] ==NULL) Response.Redirect ("Default.aspx"); //if (application["chat"] = = null)//application["Chat" = new Panel (); //Panel1.Controls.Add (panel) application["chat");This part of the comment above is moved to DEFAULT3. }
Then run:
After logging in two users, you can see that, even if you do not send any information, after 5 Seconds of time, automatically refresh, will not be like before to jump back to the login page, and after sending the message, automatic refresh will not be the contents of the text box to clear out.
ASP. Application