找回密码
 会员注册
查看: 112|回复: 0

ASP.NET实现简单的聊天室

[复制链接]

1389

主题

5

回帖

496万

积分

管理员

积分
4962990
发表于 2024-2-29 08:47:47 | 显示全部楼层 |阅读模式

目录

Default.aspx

Default.aspx.cs

main.aspx

main.aspx

SendMessage.aspx

SendMessage.aspx.cs 

ShowMessage.aspx

ShowMessage.aspx.cs

Users.aspx 

Users.aspx .cs

运行结果


Default.aspx

  1. <body>
  2. <form id="form1" runat="server">
  3. <div style="text-align:center">
  4. <br />
  5. 登陆<br />
  6. <br />
  7. 用户名:<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>&nbsp;
  8. <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="登 陆" Width="69px" />
  9. <asp:Label ID="lblTs" runat="server" ForeColor="Red" Height="13px" Text="用户名太短" Visible="false" Width="112px"></asp:Label>
  10. <br />
  11. </div>
  12. </form>
  13. </body>
复制代码

Default.aspx.cs

  1. protected void btnLogin_Click(object sender, EventArgs e)
  2. {
  3. bool IsChonfu = false;
  4. if (this.txtUsername.Text.Trim().Length < 1)
  5. {
  6. lblTs.Visible = true;
  7. }
  8. else
  9. {
  10. string UserIp = Request.UserHostAddress.ToString();
  11. Session["User"] = this.txtUsername.Text.Trim();
  12. string UserInfo = this.txtUsername.Text.Trim() + "--" + UserIp;
  13. ArrayList UserList;
  14. if (Application["UserList"] == null)
  15. {
  16. UserList = new ArrayList();
  17. UserList.Add(UserInfo);
  18. Application["UserList"] = UserList;
  19. Response.Redirect("main.aspx");
  20. }
  21. else
  22. {
  23. UserList = (ArrayList)Application["UserList"];
  24. for (int i = 0; i <UserList.Count; i++)
  25. {
  26. if (UserInfo==UserList[i].ToString())
  27. {
  28. IsChonfu = true;
  29. }
  30. else
  31. {
  32. IsChonfu = false;
  33. }
  34. }
  35. if (IsChonfu==false)
  36. {
  37. UserList.Add(UserInfo);
  38. }
  39. Application["UserList"] = UserList;
  40. Response.Redirect("main.aspx");
  41. }
  42. }
  43. }
复制代码

main.aspx

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <title>聊天室主页</title>
  5. <style type="text/css">
  6. #iframe1{left:0px;width:1000px;height:400px;}
  7. #iframe2{right:0px;height:400px}
  8. #iframe3{bottom:0px;width:1200px;}
  9. </style>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <div id="div1">
  14. <iframe id="iframe1" src="ShowMessage.aspx"></iframe>
  15. <iframe id="iframe2" src="Users.aspx"></iframe>
  16. <iframe id="iframe3" src="SendMessage.aspx"></iframe>
  17. </div>
  18. </form>
  19. </body>
  20. </html>
复制代码

main.aspx

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Session["User"] == null)
  4. {
  5. Response.Redirect("Default.aspx");
  6. }
  7. }
复制代码

SendMessage.aspx

  1. <body>
  2. <form id="form1" runat="server">
  3. <div align="center">
  4. <br />
  5. 发送消息
  6. <asp:TextBox ID="txtMessage" runat="server" Height="52px" Width="596px"></asp:TextBox>
  7. <asp:Button ID="btnSend" runat="server" Height="61px" Text="立 即 发 送" Width="97px" OnClick="btnSend_Click1" />
  8. <asp:Button ID="bthDeleteMessage" runat="server" Height="61px" OnClick="btnDeleteMessage_Click"
  9. Text="删除聊天记录" Width="97px" />&nbsp;
  10. <asp:Button ID="btnDeleteUsers" runat="server"
  11. Height="61px" OnClick="btnDeleteUsers_Click1" Text="清除在线用户" Width="97px" /><br />
  12. <br />
  13. </div>
  14. </form>
  15. </body>
复制代码

SendMessage.aspx.cs 

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Session["User"] == null)
  4. {
  5. Response.Redirect("Default.aspx");
  6. }
  7. }
  8. protected void btnSend_Click1(object sender, EventArgs e)
  9. {
  10. ArrayList MessageList = new ArrayList();
  11. string SendUser = Session["User"].ToString();
  12. string SendMessage = this.txtMessage.Text;
  13. string SendTime = DateTime.Now.ToString();
  14. string Message = SendUser + "于" + SendTime + "说:" + SendMessage + "<br><br>";
  15. if (Application["MessageList"] == null)
  16. {
  17. MessageList.Add(Message);
  18. Application["MessageList"] = MessageList;
  19. }
  20. else
  21. {
  22. MessageList = (ArrayList)Application["MessageList"];
  23. MessageList.Add(Message);
  24. Application["MessageList"] = MessageList;
  25. }
  26. this.txtMessage.Text = "";
  27. this.txtMessage.Focus();
  28. }
  29. protected void btnDeleteMessage_Click(object sender, EventArgs e)
  30. {
  31. Application.Remove("MessageList");
  32. }
  33. protected void btnDeleteUsers_Click1(object sender, EventArgs e)
  34. {
  35. Application.Remove("UserList");
  36. Response.Redirect("Default.aspx");
  37. }
复制代码

ShowMessage.aspx

  1. <head runat="server">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3. <title>聊天室</title>
  4. <style type="text/css">
  5. body,td,th {
  6. font-size: 16px;
  7. color:#FF0000;
  8. font-weight:bold;
  9. padding:inherit;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <script>setTimeout("location.href='ShowMessage.aspx'",900)</script>
  15. <form id="form1" runat="server">
  16. <div>
  17. <%
  18. ArrayList MessageList = new ArrayList();
  19. if(Application["MessageList"]==null)
  20. {
  21. Response.Write("暂无聊天信息");
  22. }
  23. else
  24. {
  25. MessageList = (ArrayList)Application["MessageList"];
  26. for(int i=0;i<MessageList.Count; i++)
  27. {
  28. Response.Write(MessageList[i]);
  29. }
  30. }
  31. %>
  32. </div>
  33. </form>
  34. </body>
复制代码

ShowMessage.aspx.cs

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Session["User"] == null)
  4. {
  5. Response.Redirect("Default.aspx");
  6. }
  7. }
复制代码

Users.aspx 

  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <span style="color: #ff0066">&nbsp; &nbsp; &nbsp; 用户列表<br />
  5. <br />
  6. <%
  7. ArrayList UserList = new ArrayList();
  8. if(Application["UserList"]==null)
  9. {
  10. Response.Write("暂无用户");
  11. }
  12. else
  13. {
  14. UserList = (ArrayList)Application["UserList"];
  15. for(int i = 0; i < UserList.Count; i++)
  16. {
  17. Response.Write(UserList[i] + "<br><br>");
  18. }
  19. }
  20. %>
  21. </span>
  22. <br />
  23. <br />
  24. </div>
  25. </form>
  26. </body>
复制代码

Users.aspx .cs

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Session["User"] == null)
  4. {
  5. Response.Redirect("Default.aspx");
  6. }
  7. }
复制代码

运行结果


来源:https://blog.csdn.net/qq_40323256/article/details/83934458
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?会员注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-26 12:54 , Processed in 0.333172 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表