用Asp.net做一个简易聊天室
- 在解决方案中分别创建3个web窗体以及全局变量Global.asax,用于用户登录:ChatLogin.aspx ,用于显示和发送信息:Chat.aspx ,用于发送异步请求:Ajax.aspx
- 安装并配置jQuery
- 添加控件,完成属性,完成代码
运行效果图:
ChatLogin.aspx代码:
我的聊天室 | ||
用户名: | * | |
密码: | * | |
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">