Aplication对象 简单聊天室(16)
生活随笔
收集整理的這篇文章主要介紹了
Aplication对象 简单聊天室(16)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Application對象可以使得多個用戶在訪問同一個asp.net應用程序時,能夠共享信息,在多個用戶同時訪問asp.net時,都會 產生一個Application對象。通常可以把共享的數據變量存儲在Application對象的集合中,這些變量可以在站點應用程序所有的頁面上被所 有的客戶端訪問,這樣就是所有的客戶端公用一個Application對象,網站服務器被開啟時,Application就會被創建,利用共享這一特性, 我們可以創建簡單的聊天室以及網站計數器。 Application對象常用的方 法:
Application最常見的應用就是聊天室,下面創建一個簡單的聊天室演示Application的應用。 html代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Application._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>聊天室</title>
<meta http-equiv="refresh" content="5;url=Default.aspx"/>
</head>
<body>
<form id="form1" runat="server" method="post">
<div style="text-align:center">簡單的聊天室</div>
<hr style="color:Red"/>
聊天內容:<br />
<%Response.Write(Application["chat"]); %><!通過response對象的write方法將application對象中存儲的聊天信息進行輸出>
<hr style="color:Blue"/>
小敏對
<asp:DropDownList ID="friend" runat="server" Width="90px">
<asp:ListItem Value="小李">小李</asp:ListItem>
<asp:ListItem Value="小軍">小軍</asp:ListItem>
<asp:ListItem Value="小麗">小麗</asp:ListItem>
</asp:DropDownList>說:
<asp:TextBox ID="content" runat="server" Height="127px" Width="263px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="提交" οnclick="Button1_Click1" />
</form>
</body>
</html> html代碼中<meta? http-equiv="refresh" content="5;url=Default.aspx"/>意思是網頁每五秒刷新一次,并跳轉到Default.aspx頁面 c#后臺代碼: using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Application
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string name = "小敏";
string char_str = name + "對" + Request["friend"] + "說:" + Request["content"] + "<br/>";
Application.Lock();//先用Lock將application鎖住,進行修改;
Application["chat"] = Application["chat"] + char_str;
Application.UnLock();//然后再把application對象鎖打開;
this.content.Controls.Clear();//點擊“提交”按鈕提交信息后自動清除用戶輸入內容;
}
}
} 當用戶點擊“提交”按鈕時,會觸發一個事件,該事件會把用戶輸入的信息保存到Application對象中,有因為Application對象是一個保存 共享信息的對象,所以每個用戶想修改信息時,就會出現混亂,導致數據不一致,為了防止此問題的發生,這里利用了lock和unlock方法,相擁lock 把Application鎖住,再進行修改,最后使用unlock把鎖打開; 用火狐打開預覽的效果圖:
本文轉自shenzhoulong ?51CTO博客,原文鏈接:http://blog.51cto.com/shenzhoulong/313780,如需轉載請自行聯系原作者
| 方法 | 說明 |
| Add | 向Application添加新的對象 |
| Clear | 移除Application中所有的對象 |
| Remove | 按名稱移除Application集合中對象 |
| Lock | 禁止其他用戶修Application對象記錄的變量值 |
| Unlock | 允許其他用戶修Application對象記錄的變量值 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>聊天室</title>
<meta http-equiv="refresh" content="5;url=Default.aspx"/>
</head>
<body>
<form id="form1" runat="server" method="post">
<div style="text-align:center">簡單的聊天室</div>
<hr style="color:Red"/>
聊天內容:<br />
<%Response.Write(Application["chat"]); %><!通過response對象的write方法將application對象中存儲的聊天信息進行輸出>
<hr style="color:Blue"/>
小敏對
<asp:DropDownList ID="friend" runat="server" Width="90px">
<asp:ListItem Value="小李">小李</asp:ListItem>
<asp:ListItem Value="小軍">小軍</asp:ListItem>
<asp:ListItem Value="小麗">小麗</asp:ListItem>
</asp:DropDownList>說:
<asp:TextBox ID="content" runat="server" Height="127px" Width="263px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="提交" οnclick="Button1_Click1" />
</form>
</body>
</html> html代碼中<meta? http-equiv="refresh" content="5;url=Default.aspx"/>意思是網頁每五秒刷新一次,并跳轉到Default.aspx頁面 c#后臺代碼: using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Application
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string name = "小敏";
string char_str = name + "對" + Request["friend"] + "說:" + Request["content"] + "<br/>";
Application.Lock();//先用Lock將application鎖住,進行修改;
Application["chat"] = Application["chat"] + char_str;
Application.UnLock();//然后再把application對象鎖打開;
this.content.Controls.Clear();//點擊“提交”按鈕提交信息后自動清除用戶輸入內容;
}
}
} 當用戶點擊“提交”按鈕時,會觸發一個事件,該事件會把用戶輸入的信息保存到Application對象中,有因為Application對象是一個保存 共享信息的對象,所以每個用戶想修改信息時,就會出現混亂,導致數據不一致,為了防止此問題的發生,這里利用了lock和unlock方法,相擁lock 把Application鎖住,再進行修改,最后使用unlock把鎖打開; 用火狐打開預覽的效果圖:
本文轉自shenzhoulong ?51CTO博客,原文鏈接:http://blog.51cto.com/shenzhoulong/313780,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Aplication对象 简单聊天室(16)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习-Python函数之函数定义与调用之
- 下一篇: 莱姆智慧城市新愿景 用匠人精神助飞智能电