(ASP.net)利用Application对象制作简单聊天室_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > (ASP.net)利用Application对象制作简单聊天室

(ASP.net)利用Application对象制作简单聊天室

 2013/11/22 15:45:28  妮妮盘  博客园  我要评论(0)
  • 摘要:1.共四个页面,Default.aspx默认主页,Default2.aspx聊天室default3.aspx显示用户列表,default4.aspx显示聊天内容,添加一个Global.asax全局程序集文件,初始化Application对象值2.在global.asax中voidApplication_Start(objectsender,EventArgse){//在应用程序启动时运行的代码//建立用户列表stringuser=""
  • 标签:.net ASP.NET net 利用 Application对象 APP

1.共四个页面,Default.aspx默认主页,Default2.aspx聊天室 default3.aspx显示用户列表,default4.aspx显示聊天内容,添加一个Global.asax全局程序集文件,初始化 Application对象

2.在global.asax中

  void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        //建立用户列表
        string user = "";//用户列表
        Application["user"] = user;
        Application["userNum"] = 0;
        string chats = "";//聊天记录  
        Application["chats"] = chats;
        Application["current"] = 0;//聊天记录数
    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  在应用程序关闭时运行的代码
        Application["user"] = "";
        Application["chats"] = "";
    }

3.DEFAULT.ASPX中 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int p_judge = 0;
        p_judge = Convert.ToInt32(Request["value"]);
        if (!IsPostBack)
        {
            if (p_judge == 1)
                Response.Write("<script>alert('该用户已经存在')</script>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application.Lock();
        int p_num;//在线人数
        string p_name;//登录用户
        string p_names;//已在线用户
        string[] p_users;//用户在线数组
        p_num = int.Parse(Application["userNum"].ToString());
        if (TextBox1.Text == "")
        {
            Response.Write("<script>alert('用户名不能为空')</script>");
            TextBox1.Focus();
        }
        else
        {
            p_name = TextBox1.Text.Trim();
            p_names = Application["user"].ToString();
            p_users = p_names.Split(',');
            for (int i = 0; i <= p_num - 1; i++)
            {
                if (p_name == p_users[i].Trim())
                {
                    int p_judge = 1;
                    Response.Redirect("Default.aspx?value="+p_judge);
                }
            }
            if (p_num == 0)
                Application["user"] = p_name.ToString();
            else
                Application["user"] = Application["user"] + "," + p_name.ToString();
            p_num += 1;
            Application["userNum"] = p_num;
            Session["userName"] = TextBox1.Text.Trim();
            Application.UnLock();
            Response.Redirect("Default2.aspx");
        }
    }

4.default2.aspx 采用 frame 的SRC 属性来显示default3 default4

 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["userName"].ToString();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int p_current = int.Parse(Application["current"].ToString());
        Application.Lock();
        if (p_current == 0 || p_current > 20)
        {
            p_current = 0;
            Application["chats"] = Session["userName"].ToString() + "说:" + TextBox1.Text.Trim() + "(" + DateTime.Now.ToString() + ")";

        }
        else
        {
            Application["chats"] = Application["chats"].ToString() + "," + Session["userName"].ToString() + "说:" + TextBox1.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
        }
        p_current += 1;
        Application["current"] = p_current;
        Application.UnLock();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close()</script>");
    }

5.default3.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList ItemList = new ArrayList();
        Application.Lock();
        string p_names;
        string[] p_user;
        int p_num = int.Parse(Application["userNum"].ToString());
        p_names = Application["user"].ToString();
        p_user = p_names.Split(',');
        for (int i = (p_num - 1); i >= 0; i--)
        {
            ItemList.Add(p_user[i].ToString());
        }
        ListBox1.DataSource = ItemList;
        ListBox1.DataBind();
        Application.UnLock();
    }

6.defaut4.aspx

 protected void Page_Load(object sender, EventArgs e)
    {
        int p_current = int.Parse(Application["current"].ToString());
        Application.Lock();
        string p_chats = Application["chats"].ToString();
        string[] p_chat = p_chats.Split(',');
        for (int i = p_chat.Length - 1; i >= 0; i--)
        {
            if (p_current == 0)
            {
                TextBox1.Text = p_chat[i].ToString();
            }
            else
            {
                TextBox1.Text = TextBox1.Text + "/n" + p_chat[i].ToString();

            }
        }
        Application.UnLock();
    }

 

发表评论
用户名: 匿名