C#实例之简单聊天室(状态管理)
前言
状态管理是在同一页或不同页的多个请求发生时,维护状态和页信息的过程。因为Web应用程序的通信协议使用了无状态的HTTP协议,所以当客户端请求页面时,ASP.NET服务器端都会重新生成一个网页实例。此时,旧网页的任务完成,旧网页的实例也随之消失。这种无状态,意味着客户端用户在浏览器中的一些状态或是对数据的一些修改都将丢失。
为了弥补这种基于web应用程序的固有限制,ASP.NET提供了多种用于管理状态的功能。
简单聊天室
这里运用System.Web命名空间的那些管理状态的类,做一个简单的聊天室
0.效果图

【 不知道为什么下拉框DropDownList1的选中项老是获取不了,有谁知道的还请告诉我一声啊,哈哈哈】
1.Default.aspx页面:用于用户登录,一个用户文本框,一个登录按钮,给登录按钮添加事件
后台主要代码
protected void Button1_Click(object sender, EventArgs e){Application.Lock();int intUserNum;string user;string[] users;string strUserName;intUserNum = Convert.ToInt32(Application["userNum"]);if (intUserNum > 20){Response.Write("在线人数超过20人,不能登录了!");Response.Redirect("Default.aspx");}else {user = Application["user"].ToString();users = user.Split(',');strUserName = TextBox1.Text.Trim();for (int i = 0; i < intUserNum; i++) {if (users[i] == strUserName){Response.Redirect("Default.aspx?value=1");}}if (users == null)Application["user"] = strUserName;elseApplication["user"] += "," + strUserName;intUserNum += 1;Application["userNum"] = intUserNum.ToString();Session["user"] = strUserName;Application.UnLock();Response.Redirect("main.aspx"); ;}}
2.Left.aspx页面:主要用于呈现在线人数和在线人员名单,由2个label和1个textbox组成
后台主要代码
public partial class Left : System.Web.UI.Page{private ArrayList ItemList = new ArrayList();protected void Page_Load(object sender, EventArgs e){Application.Lock();if (Session["user"] == null)Response.Redirect("Default.aspx");elseLabel1.Text = Session["user"].ToString();int userCount = int.Parse(Application["userNum"].ToString());Label2.Text = userCount.ToString();string user = Application["user"].ToString();string[] users = user.Split(',');for (int i = (userCount-1); i >= 0; i--) {ItemList.Add(users[i]);}ListBox1.DataSource = ItemList;ListBox1.DataBind();Application.UnLock();}}
3.Right.aspx页面:用于呈现聊天记录,由一个textbox组成
后台主要代码
protected void Page_Load(object sender, EventArgs e){Application.Lock();if (!IsPostBack) {string chat=Application["chats"].ToString();int chatNum=int.Parse(Application["current"].ToString()) ;string[] chats = chat.Split(',');for (int i = chatNum - 1; i >= 0; i--){if(chatNum==0)TextBox1.Text=chats[i];elseTextBox1.Text=TextBox1.Text+"\n"+chats[i];}}Application.UnLock();}
4.bottom.aspx页面:用于用户向其他用户发送消息,由一个下拉列表,文本框和发送按钮组成,按钮要添加事件
后台主要代码
public partial class bottom : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Application.Lock();string userName = Application["user"].ToString();string[] users = userName.Split(',');DropDownList1.DataSource = users;DropDownList1.DataBind();Application.UnLock();}protected void Button1_Click(object sender, EventArgs e){Application.Lock();int current = int.Parse(Application["current"].ToString());string liaotian = Session["user"] + "对" + DropDownList1.SelectedValue.ToString() + "说:" + TextBox1.Text.Trim() + DateTime.Now.ToString();if (current == 0 || current > 40){current = 0;Application["chats"] = liaotian;}else{Application["chats"] +=","+ liaotian;}current += 1;Application["current"] = (object)current;Application.UnLock();}}
5.main.aspx页面:用于组合前面3个页面,使用iframe
主要代码
<div><iframe id="ifraLeft" name="ifraLeft" src="Left.aspx"style="width:200px;height:500px;float:left;margin-left:10px;margin-top:10px;background-color:grey;border:2px solid #000000"></iframe><iframe id="ifraRight" name="ifraRight" src="Right.aspx"style="width:700px;height:500px;float:left;margin-top:10px;background-color:#000000;border:2px solid #000000;"></iframe><iframe id="ifrabottom" name="ifrabottom" src="bottom.aspx"style="width:900px;height:50px;margin-left:10px;margin-top:10px;background-color:red;border:2px solid #000000;"></iframe></div>

浙公网安备 33010602011771号