|
目录
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 - <body>
- <form id="form1" runat="server">
- <div style="text-align:center">
- <br />
- 登陆<br />
- <br />
- 用户名:<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
- <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="登 陆" Width="69px" />
- <asp:Label ID="lblTs" runat="server" ForeColor="Red" Height="13px" Text="用户名太短" Visible="false" Width="112px"></asp:Label>
- <br />
- </div>
- </form>
- </body>
复制代码
Default.aspx.cs - protected void btnLogin_Click(object sender, EventArgs e)
- {
- bool IsChonfu = false;
- if (this.txtUsername.Text.Trim().Length < 1)
- {
- lblTs.Visible = true;
- }
- else
- {
- string UserIp = Request.UserHostAddress.ToString();
- Session["User"] = this.txtUsername.Text.Trim();
- string UserInfo = this.txtUsername.Text.Trim() + "--" + UserIp;
- ArrayList UserList;
-
- if (Application["UserList"] == null)
- {
- UserList = new ArrayList();
- UserList.Add(UserInfo);
- Application["UserList"] = UserList;
- Response.Redirect("main.aspx");
- }
- else
- {
-
- UserList = (ArrayList)Application["UserList"];
-
- for (int i = 0; i <UserList.Count; i++)
- {
- if (UserInfo==UserList[i].ToString())
- {
- IsChonfu = true;
- }
- else
- {
- IsChonfu = false;
- }
- }
- if (IsChonfu==false)
- {
- UserList.Add(UserInfo);
- }
- Application["UserList"] = UserList;
- Response.Redirect("main.aspx");
- }
- }
- }
复制代码
main.aspx - <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>聊天室主页</title>
- <style type="text/css">
- #iframe1{left:0px;width:1000px;height:400px;}
- #iframe2{right:0px;height:400px}
- #iframe3{bottom:0px;width:1200px;}
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div id="div1">
- <iframe id="iframe1" src="ShowMessage.aspx"></iframe>
- <iframe id="iframe2" src="Users.aspx"></iframe>
- <iframe id="iframe3" src="SendMessage.aspx"></iframe>
- </div>
-
- </form>
- </body>
- </html>
复制代码
main.aspx - protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["User"] == null)
- {
- Response.Redirect("Default.aspx");
- }
- }
复制代码
SendMessage.aspx - <body>
- <form id="form1" runat="server">
- <div align="center">
- <br />
- 发送消息
- <asp:TextBox ID="txtMessage" runat="server" Height="52px" Width="596px"></asp:TextBox>
- <asp:Button ID="btnSend" runat="server" Height="61px" Text="立 即 发 送" Width="97px" OnClick="btnSend_Click1" />
- <asp:Button ID="bthDeleteMessage" runat="server" Height="61px" OnClick="btnDeleteMessage_Click"
- Text="删除聊天记录" Width="97px" />
- <asp:Button ID="btnDeleteUsers" runat="server"
- Height="61px" OnClick="btnDeleteUsers_Click1" Text="清除在线用户" Width="97px" /><br />
- <br />
- </div>
- </form>
- </body>
复制代码
SendMessage.aspx.cs - protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["User"] == null)
- {
- Response.Redirect("Default.aspx");
- }
- }
- protected void btnSend_Click1(object sender, EventArgs e)
- {
- ArrayList MessageList = new ArrayList();
- string SendUser = Session["User"].ToString();
- string SendMessage = this.txtMessage.Text;
- string SendTime = DateTime.Now.ToString();
- string Message = SendUser + "于" + SendTime + "说:" + SendMessage + "<br><br>";
- if (Application["MessageList"] == null)
- {
- MessageList.Add(Message);
- Application["MessageList"] = MessageList;
- }
- else
- {
- MessageList = (ArrayList)Application["MessageList"];
- MessageList.Add(Message);
- Application["MessageList"] = MessageList;
- }
- this.txtMessage.Text = "";
- this.txtMessage.Focus();
- }
- protected void btnDeleteMessage_Click(object sender, EventArgs e)
- {
- Application.Remove("MessageList");
- }
- protected void btnDeleteUsers_Click1(object sender, EventArgs e)
- {
- Application.Remove("UserList");
- Response.Redirect("Default.aspx");
- }
复制代码
ShowMessage.aspx - <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>聊天室</title>
- <style type="text/css">
- body,td,th {
- font-size: 16px;
- color:#FF0000;
- font-weight:bold;
- padding:inherit;
- }
- </style>
- </head>
- <body>
- <script>setTimeout("location.href='ShowMessage.aspx'",900)</script>
- <form id="form1" runat="server">
- <div>
- <%
- ArrayList MessageList = new ArrayList();
- if(Application["MessageList"]==null)
- {
- Response.Write("暂无聊天信息");
- }
- else
- {
- MessageList = (ArrayList)Application["MessageList"];
- for(int i=0;i<MessageList.Count; i++)
- {
- Response.Write(MessageList[i]);
- }
- }
- %>
- </div>
- </form>
- </body>
复制代码
ShowMessage.aspx.cs - protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["User"] == null)
- {
- Response.Redirect("Default.aspx");
- }
- }
复制代码
Users.aspx - <body>
- <form id="form1" runat="server">
- <div>
- <span style="color: #ff0066"> 用户列表<br />
- <br />
- <%
- ArrayList UserList = new ArrayList();
- if(Application["UserList"]==null)
- {
- Response.Write("暂无用户");
- }
- else
- {
- UserList = (ArrayList)Application["UserList"];
- for(int i = 0; i < UserList.Count; i++)
- {
- Response.Write(UserList[i] + "<br><br>");
- }
- }
- %>
- </span>
- <br />
- <br />
- </div>
- </form>
- </body>
复制代码
Users.aspx .cs - protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["User"] == null)
- {
- Response.Redirect("Default.aspx");
- }
- }
复制代码
运行结果
来源:https://blog.csdn.net/qq_40323256/article/details/83934458 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?会员注册
×
|