利用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/