利用asp的application全局属性 做网络聊天室

来源:互联网 发布:同步带传动计算软件 编辑:程序博客网 时间:2024/06/01 18:35

3个TextBox 分辨命名 chatbox  namebox  msgbox

本来还想调用定时刷新的 还没有学会


Application.Lock();

Application.UnLock();

是为了防止多人同时读写 出bug

asp 路漫漫 吾上下求索



using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HelloDuiXiang
{
    public partial class TestApplication : System.Web.UI.Page
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["chatdata"] = null;
            Application["chatnum"] = 0;
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Application["chatdata"] = null;
            Application["chatnum"] = 0;
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack==true)
            {
                int i = 0,sum=0;
                for (i = 0; i < 100; i++) sum += i;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if(namebox .Text!="" && msg.Text!="")
            {
                Application.Lock();
                Application["chats"] = namebox .Text + ":" + msg.Text + "\r\n" + Application["chats"];
                Application.UnLock();
                chatBox.Text = Application["chats"].ToString();
            }
            else Response.Write("<script>alert('测试')</script>");
        }

        //protected void Timer1_Tick(object sender, EventArgs e)
        //{
        //    chatBox.Text = Application["chats"].ToString();
        //}
    }
}