简单聊天室的代码

简单的聊天室代码-.net(c#)

vs2010

1.       新建网站-空网站

2.       添加4WEB窗体新项、1个全局应用程序类新项

3.       源代码:

Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>登录</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <table align="center" border="1" cellpadding="0" cellspacing="0" style="width: 199px;

            height: 100px">

            <tr>

                <td colspan="2" style="font-weight: bold; font-size: 16pt; color: #ffffff; background-color: #54a4ff;

                    text-align: center">

                   登录</td>

            </tr>

            <tr>

                <td style="font-size: 9pt; background-color: #f7f6f3; text-align: right">

                    用户名:êo</td>

                <td style="background-color: #f7f6f3; text-align: center">

                    <asp:TextBox ID="TextBox1" runat="server" Width="113px"></asp:TextBox></td>

            </tr>

            <tr>

                <td style="background-color: #f7f6f3">

                </td>

                <td style="background-color: #f7f6f3; text-align: center">

                    <asp:Button ID="btnLogin" runat="server" Font-Size="9pt" OnClick="btnLogin_Click"

                        Text="登录" />

                    &nbsp;

                    <asp:Button ID="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 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("<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" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>聊天室</title>

</head>

<body>

    <form id="form1" runat="server">

    <div style="text-align: center">

        <table style="width: 603px; height: 442px" border="1" cellpadding="0" cellspacing="0">

            <tr>

                <td colspan="2" style="height: 51px; font-size: 16pt; color: #ffffff; background-color: #54a4ff;">

                    聊天室</td>

            </tr>

            <tr>

                <td style="width: 404px; height: 18px">

                </td>

                <td align="left" style="width: 200px; height: 18px; font-size: 9pt; border-right-style: none; border-left-style: none;">

                    用户列表</td>

            </tr>

            <tr>

                <td style="width: 404px; height: 354px;">

                <iframe id="Iframe1" src="Content.aspx" runat="server" scrolling="no" noresize="noResize" frameborder="0" style="width: 400px; height: 350px"></iframe>

                </td>

                <td style="width: 200px; height: 354px;">

                <iframe id="Iframe2" src="List.aspx" runat="server" frameborder="no" noresize="noResize" scrolling="no" style="width: 200px; height: 350px"></iframe>

                </td>

            </tr>

            <tr>

                <td style="width: 400px" align="left">

                    <asp:TextBox ID="txtMessage" runat="server" Width="316px"></asp:TextBox><asp:Button ID="btnSend" runat="server" Text="发送" OnClick="btnSend_Click" /><asp:Button ID="btnExit" runat="server" Text="退出" OnClick="btnExit_Click" /></td>

                <td style="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 partial class _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" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>无标题页</title>

     <meta http-equiv="refresh" content="1" />

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="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 partial class 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" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>无标题页</title>

     <meta http-equiv="refresh" content="1" />

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ListBox ID="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 partial class 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#" %>

 

<script runat="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>

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值