用Asp.net做一个简易聊天室

用Asp.net做一个简易聊天室

  1. 在解决方案中分别创建3个web窗体以及全局变量Global.asax,用于用户登录:ChatLogin.aspx ,用于显示和发送信息:Chat.aspx ,用于发送异步请求:Ajax.aspx
  2. 安装并配置jQuery
  3. 添加控件,完成属性,完成代码
    运行效果图:
    在这里插入图片描述
    在这里插入图片描述
    ChatLogin.aspx代码:
 我的聊天室 
用户名: *
密码: *
***ChatLogin.aspx.cs代码:** public partial class ChatLogin : System.Web.UI.Page { string[,] user = { { "张三", "111111" }, { "王五", "111111" }, { "李四", "111111" } };
protected TextBox GetTxtPassword()
{
    return txtPassword;
}

protected void Page_Load(object sender, EventArgs e, TextBox txtPassword)
{

}

protected void btnLogin_Click(object sender, EventArgs e)
{
    for (int i = 0; i <= 2; i++)
    {
        if (txtName.Text == user[i, 0] && txtPassword.Text == user[i, 1])
        {
            Session["user"] = user[i, 0];
            Response.Redirect("Chat.aspx");
            //Response.Cookies["user"].Value = "i";


        }
        txtName.Focus();
        if (txtName.Text == "user" && txtPassword.Text == "111111")
        {
            HttpCookie cookie = new HttpCookie("user");
            cookie.Value = "user";
            cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);


        }

    }
    Response.Write("<script type='text/javascript'>alert('用户名或密码错误!’)</script>");
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    for (int i = 0; i <= 2; i++)
    {
        if (txtName.Text == user[i, 0] && txtPassword.Text == user[i, 1])
        {
            Session["user"] = user[i, 0];
            Response.Redirect("Chat.aspx");
            //Response.Cookies["user"].Value = "i";


        }
        txtName.Focus();
        if (txtName.Text == "user" && txtPassword.Text == "111111")
        {
            HttpCookie cookie = new HttpCookie("user");
            cookie.Value = "user";
            cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);


        }
        
    }
}

}
***Chat.aspx代码

在这里插入代码片    <title></title>
    <script src="Scripts/jquery-3.6.0.min.js"></script>
    <%--  refresh()函数以500毫秒,连续的局部刷新div层divMsg。其中$.ajax用JQuery提供的ajax()方法,用于执行异步请求--%>
    <script type="text/javascript">
        function refresh() {
            $.ajax({
                url: "Ajax.aspx",
                caches: false,
                success: function (text) {
                    document.getElementById("divMsg").innerHTML = text;

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("网络链接有问题,请重试!");
                }
            });
            setTimeout("refresh()", 500);
            }

    </script>

</head>
<body onload="refresh()">
    <form id="form1" runat="server">
        <div id ="divMsg">  </div>
            <div>
             <asp:Label ID="lblName" runat="server"></asp:Label>
              <br />
            <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server" Height="182px" Width="251px"></asp:TextBox>
              <br />
            <asp:Button ID="btnSend"  runat="server" Text="发送" OnClick="btnSend_Click" />
        </div>


    </form>
</body>

***Chat.aspx后台代码

在这里插入代码片 protected void Page_Load(object sender, EventArgs e)
    {
        lblName.Text = "发言人:" + Session["user"];
        if (!IsPostBack)
        {
            Application["message"] = Session["user"] + "进入聊天室<br />"
                + Application["message"];

        }
    }
 protected void btnSend_Click(object sender, EventArgs e)
    {
        Application.Lock();
        Application["message"] = Session["user"] + "说:" + txtMessage.Text
            + "(" + DateTime.Now.ToString() + ")<br />" + Application["message"];
        Application.UnLock();
        txtMessage.Text = "";
    }

Ajax.aspx保留@Page指令行,删除其他的XHTM5元素

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

Ajax.aspx后台代码

在这里插入代码片public partial class Ajax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Application["message"].ToString());
    }
    Request.Server Variables["Remote_ADDR"];
}
  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值