简单聊天室的代码
来源:互联网 发布:进入recovery模式软件 编辑:程序博客网 时间:2024/05/24 22:43
简单的聊天室代码-.net(c#)
vs2010
1. 新建网站-空网站
2. 添加4个WEB窗体新项、1个全局应用程序类新项
3. 源代码:
Login.aspx
<%@Page Language="C#"AutoEventWireup="true"CodeFile="Login.aspx.cs"Inherits="Login" %>
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>登录</title>
</head>
<body>
<formid="form1" runat="server">
<div>
<tablealign="center" border="1" cellpadding="0"cellspacing="0" style="width: 199px;
height: 100px">
<tr>
<tdcolspan="2" style="font-weight: bold;font-size: 16pt; color: #ffffff; background-color: #54a4ff;
text-align: center">
登录</td>
</tr>
<tr>
<tdstyle="font-size: 9pt;background-color: #f7f6f3;text-align: right">
用户名:êo</td>
<tdstyle="background-color: #f7f6f3;text-align: center">
<asp:TextBoxID="TextBox1" runat="server" Width="113px"></asp:TextBox></td>
</tr>
<tr>
<tdstyle="background-color: #f7f6f3">
</td>
<tdstyle="background-color: #f7f6f3;text-align: center">
<asp:ButtonID="btnLogin" runat="server" Font-Size="9pt"OnClick="btnLogin_Click"
Text="登录"/>
<asp:ButtonID="btnExit" runat="server" Font-Size="9pt"OnClick="btnExit_Click"Text="退出"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Login.aspx.CS
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 partialclass 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("<script>alert('该用户已登录')</script>");
}
}
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("<script>alert('用户名不能为空')</script>");
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("Default.aspx");
}
}
protected void btnExit_Click(object sender, EventArgs e)
{
Response.Write("<script>window.close();</script>");
}
}
Default.aspx
<%@Page Language="C#"AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>聊天室</title>
</head>
<body>
<formid="form1" runat="server">
<divstyle="text-align: center">
<tablestyle="width: 603px;height: 442px"border="1" cellpadding="0" cellspacing="0">
<tr>
<tdcolspan="2" style="height: 51px;font-size: 16pt; color: #ffffff; background-color: #54a4ff;">
聊天室</td>
</tr>
<tr>
<tdstyle="width: 404px;height: 18px">
</td>
<tdalign="left" style="width: 200px;height: 18px; font-size: 9pt; border-right-style: none;border-left-style: none;">
用户列表</td>
</tr>
<tr>
<tdstyle="width: 404px;height: 354px;">
<iframeid="Iframe1" src="Content.aspx" runat="server" scrolling="no"noresize="noResize"frameborder="0" style="width: 400px;height: 350px"></iframe>
</td>
<tdstyle="width: 200px;height: 354px;">
<iframeid="Iframe2" src="List.aspx" runat="server"frameborder="no" noresize="noResize" scrolling="no" style="width: 200px;height: 350px"></iframe>
</td>
</tr>
<tr>
<tdstyle="width: 400px"align="left">
<asp:TextBoxID="txtMessage" runat="server" Width="316px"></asp:TextBox><asp:ButtonID="btnSend" runat="server" Text="发送"OnClick="btnSend_Click"/><asp:ButtonID="btnExit" runat="server" Text="退出"OnClick="btnExit_Click"/></td>
<tdstyle="width: 200px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
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 partialclass _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
int P_int_current = Convert.ToInt32(Application["current"]);
Application.Lock();
if (P_int_current == 0 || P_int_current > 20)
{
P_int_current = 0;
Application["chats"] = Session["userName"].ToString() +"说" + txtMessage.Text.Trim() +"(" + DateTime.Now.ToString() +")";
}
else
{
Application["chats"] = Application["chats"].ToString() +"," + Session["userName"].ToString() +"说" + txtMessage.Text.Trim() +"(" + DateTime.Now.ToString() +")";
}
P_int_current += 1;
Application["current"] = P_int_current;
Application.UnLock();
}
protected void btnExit_Click(object sender, EventArgs e)
{
Application.Lock();
string P_str_userName = Application["user"].ToString();
Application["user"] = P_str_userName.Replace(Session["userName"].ToString(),"");
Application.UnLock();
Response.Write("<script>window.opener=null;window.close();</script>");
}
}
Content.aspx
<%@Page Language="C#"AutoEventWireup="true"CodeFile="Content.aspx.cs"Inherits="Content"%>
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
<metahttp-equiv="refresh"content="1" />
</head>
<body>
<formid="form1" runat="server">
<div>
<asp:TextBoxID="txtContent" runat="server" Height="345px"TextMode="MultiLine"Width="380px"></asp:TextBox></div>
</form>
</body>
</html>
Content.aspx.CS
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 partialclass Content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int P_int_current = Convert.ToInt32(Application["current"]);
Application.Lock();
string P_str_chats = Application["chats"].ToString();
string[] P_str_chat = P_str_chats.Split(',');
for (int i = P_str_chat.Length - 1; i >= 0; i--)
{
if (P_int_current == 0)
{
txtContent.Text = P_str_chat[i].ToString();
}
else
{
txtContent.Text = txtContent.Text + "\n" + P_str_chat[i].ToString();
}
}
Application.UnLock();
}
}
List.aspx
<%@Page Language="C#"AutoEventWireup="true"CodeFile="List.aspx.cs"Inherits="List" %>
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
<metahttp-equiv="refresh"content="1" />
</head>
<body>
<formid="form1" runat="server">
<div>
<asp:ListBoxID="lbList" runat="server" Height="345px"Width="180px"></asp:ListBox></div>
</form>
</body>
</html>
List.aspx.CS
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 partialclass List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArrayList ItemList = new ArrayList();
Application.Lock();
string P_str_names; /已经在线用户名
string[] P_str_user; //用户数组
int P_int_num =Convert.ToInt32(Application["userNum"]);
P_str_names = Application["user"].ToString();
P_str_user = P_str_names.Split(',');
for (int i = (P_int_num - 1); i >= 0; i--)
{
if(P_str_user[i].ToString ()!="")
ItemList.Add(P_str_user[i].ToString());
}
lbList.DataSource = ItemList;
lbList.DataBind();
Application.UnLock();
}
}
Global.asax
<%@Application Language="C#"%>
<scriptrunat="server">
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"] ="";
}
void Application_Error(object sender,EventArgs e)
{
}
void Session_Start(object sender,EventArgs e)
{
}
void Session_End(object sender,EventArgs e)
{
}
</script>
- 简单聊天室的代码
- 简单聊天室代码
- 简单聊天室部分代码
- 网络聊天室简单代码
- 【聊天室】android 简单的聊天室
- 关于一个asp的简单聊天室的代码问题
- Socket.io文字直播聊天室的简单代码
- 简单的java聊天室
- unity3D简单的聊天室
- unity3D简单的聊天室
- 简单的聊天室制作
- 简单聊天室的实现
- java简单的聊天室
- 简单的网络聊天室
- 一个简单的聊天室
- 简单的聊天室
- 简单的网络聊天室
- 简单聊天室的编写
- 程序员进阶书籍介绍推荐
- HDU 3555 Bomb(数位DP)
- spring(AOP)Aop的概念
- 分隔符匹配
- 欢迎使用CSDN-markdown编辑器
- 简单聊天室的代码
- 算法练习 动态规划 硬币找零
- Yii 玩转Databases
- AdaBoost
- nyoj 47
- hdoj 2068 RPG的错排
- OpenCv皮肤检测-HSV分离
- 在Mac OSX系统的Docker机上启用Docker远程API功能
- BZOJ_P1507[NOI2003]Editor(块状链表)