程序实例 -社区对话聊天asp.net

利用Application对象实现聊天功能,
Application对象为web应用提供全局变量
Application["变量名"]=表达式
比昂两名=Application["变量名"];
Application.Lock();
Application.UnLock();

codego.net 聊天界面如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int P_int_judge = 0;
        P_int_judge = Convert.ToInt32(Request["value"]);
        if (!IsPostBack)
        {
            if (P_int_judge == 1)
                Response.Write("");
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Application.Lock();
        int P_int_num;     //在线人数
        string P_str_name; //登录用户
        string P_str_names;  //已在线的用户名
        string[] P_str_user; //用户在线数组
        P_int_num = int.Parse(Application["userNum"].ToString());
        if (TextBox1.Text == "")
        {
            Response.Write("");
            TextBox1.Focus();
        }
        else
        {
            P_str_name = TextBox1.Text.Trim();
            P_str_names = Application["user"].ToString();
            P_str_user = P_str_names.Split(',');
            for (int i = 0; i <= P_int_num - 1; i++)
            {
                if (P_str_name == P_str_user[i].Trim())
                {
                    int P_int_judge = 1;
                    Response.Redirect("Login.aspx?value=" + P_int_judge);                
                }
            }
            if (P_int_num == 0)
                Application["user"] = P_str_name.ToString();
            else
                Application["user"] = Application["user"] + "," + P_str_name.ToString();
            P_int_num += 1;
            Application["userNum"] = P_int_num;
            Session["userName"] = TextBox1.Text.Trim();
            Application.UnLock();
            Response.Redirect("Chat.aspx");
        }
    }
    protected void btnExit_Click(object sender, EventArgs e)
    {
        Response.Write("");
    }
}

codego.net/tags/11/1/ 聊天登录程序如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int P_int_judge = 0;
        P_int_judge = Convert.ToInt32(Request["value"]);
        if (!IsPostBack)
        {
            if (P_int_judge == 1)
                Response.Write("");
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Application.Lock();
        int P_int_num;     //在线人数
        string P_str_name; //登录用户
        string P_str_names;  //已在线的用户名
        string[] P_str_user; //用户在线数组
        P_int_num = int.Parse(Application["userNum"].ToString());
        if (TextBox1.Text == "")
        {
            Response.Write("");
            TextBox1.Focus();
        }
        else
        {
            P_str_name = TextBox1.Text.Trim();
            P_str_names = Application["user"].ToString();
            P_str_user = P_str_names.Split(',');
            for (int i = 0; i <= P_int_num - 1; i++)
            {
                if (P_str_name == P_str_user[i].Trim())
                {
                    int P_int_judge = 1;
                    Response.Redirect("Login.aspx?value=" + P_int_judge);                
                }
            }
            if (P_int_num == 0)
                Application["user"] = P_str_name.ToString();
            else
                Application["user"] = Application["user"] + "," + P_str_name.ToString();
            P_int_num += 1;
            Application["userNum"] = P_int_num;
            Session["userName"] = TextBox1.Text.Trim();
            Application.UnLock();
            Response.Redirect("Chat.aspx");
        }
    }
    protected void btnExit_Click(object sender, EventArgs e)
    {
        Response.Write("");
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29973590/viewspace-1318334/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29973590/viewspace-1318334/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值