ASP.NET實現簡單的聊天室
阿新 • • 發佈:2018-11-13
目錄
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");
}
}