C#实例之简单聊天室(状态管理)

前言


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

简单聊天室


这里运用System.Web命名空间的那些管理状态的类,做一个简单的聊天室
0.效果图
 
【 不知道为什么下拉框DropDownList1的选中项老是获取不了,有谁知道的还请告诉我一声啊,哈哈哈】

1.Default.aspx页面:用于用户登录,一个用户文本框,一个登录按钮,给登录按钮添加事件
后台主要代码
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Application.Lock();
  4. int intUserNum;
  5. string user;
  6. string[] users;
  7. string strUserName;
  8. intUserNum = Convert.ToInt32(Application["userNum"]);
  9. if (intUserNum > 20)
  10. {
  11. Response.Write("在线人数超过20人,不能登录了!");
  12. Response.Redirect("Default.aspx");
  13. }
  14. else {
  15. user = Application["user"].ToString();
  16. users = user.Split(',');
  17. strUserName = TextBox1.Text.Trim();
  18. for (int i = 0; i < intUserNum; i++) {
  19. if (users[i] == strUserName)
  20. {
  21. Response.Redirect("Default.aspx?value=1");
  22. }
  23. }
  24. if (users == null)
  25. Application["user"] = strUserName;
  26. else
  27. Application["user"] += "," + strUserName;
  28. intUserNum += 1;
  29. Application["userNum"] = intUserNum.ToString();
  30. Session["user"] = strUserName;
  31. Application.UnLock();
  32. Response.Redirect("main.aspx"); ;
  33. }
  34. }
2.Left.aspx页面:主要用于呈现在线人数和在线人员名单,由2个label和1个textbox组成
后台主要代码
  1. public partial class Left : System.Web.UI.Page
  2. {
  3. private ArrayList ItemList = new ArrayList();
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. Application.Lock();
  7. if (Session["user"] == null)
  8. Response.Redirect("Default.aspx");
  9. else
  10. Label1.Text = Session["user"].ToString();
  11. int userCount = int.Parse(Application["userNum"].ToString());
  12. Label2.Text = userCount.ToString();
  13. string user = Application["user"].ToString();
  14. string[] users = user.Split(',');
  15. for (int i = (userCount-1); i >= 0; i--) {
  16. ItemList.Add(users[i]);
  17. }
  18. ListBox1.DataSource = ItemList;
  19. ListBox1.DataBind();
  20. Application.UnLock();
  21. }
  22. }
3.Right.aspx页面:用于呈现聊天记录,由一个textbox组成
后台主要代码
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Application.Lock();
  4. if (!IsPostBack) {
  5. string chat=Application["chats"].ToString();
  6. int chatNum=int.Parse(Application["current"].ToString()) ;
  7. string[] chats = chat.Split(',');
  8. for (int i = chatNum - 1; i >= 0; i--)
  9. {
  10. if(chatNum==0)
  11. TextBox1.Text=chats[i];
  12. else
  13. TextBox1.Text=TextBox1.Text+" "+chats[i];
  14. }
  15. }
  16. Application.UnLock();
  17. }
4.bottom.aspx页面:用于用户向其他用户发送消息,由一个下拉列表,文本框和发送按钮组成,按钮要添加事件
后台主要代码
  1. public partial class bottom : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. Application.Lock();
  6. string userName = Application["user"].ToString();
  7. string[] users = userName.Split(',');
  8. DropDownList1.DataSource = users;
  9. DropDownList1.DataBind();
  10. Application.UnLock();
  11. }
  12. protected void Button1_Click(object sender, EventArgs e)
  13. {
  14. Application.Lock();
  15. int current = int.Parse(Application["current"].ToString());
  16. string liaotian = Session["user"] + "对" + DropDownList1.SelectedValue.ToString() + "说:" + TextBox1.Text.Trim() + DateTime.Now.ToString();
  17. if (current == 0 || current > 40)
  18. {
  19. current = 0;
  20. Application["chats"] = liaotian;
  21. }else{
  22. Application["chats"] +=","+ liaotian;
  23. }
  24. current += 1;
  25. Application["current"] = (object)current;
  26. Application.UnLock();
  27. }
  28. }
5.main.aspx页面:用于组合前面3个页面,使用iframe
主要代码
  1. <div>
  2. <iframe id="ifraLeft" name="ifraLeft" src="Left.aspx"
  3. style="200px;height:500px;float:left;margin-left:10px;margin-top:10px;background-color:grey;border:2px solid #000000"></iframe>
  4. <iframe id="ifraRight" name="ifraRight" src="Right.aspx"
  5. style="700px;height:500px;float:left;margin-top:10px;background-color:#000000;border:2px solid #000000;"></iframe>
  6. <iframe id="ifrabottom" name="ifrabottom" src="bottom.aspx"
  7. style="900px;height:50px;margin-left:10px;margin-top:10px;background-color:red;border:2px solid #000000;"></iframe>
  8. </div>

案例代码下载

原文地址:https://www.cnblogs.com/iwsx/p/7096287.html