標籤:asp.net application session
login.aspx代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_chat_login.aspx.cs" Inherits="Sample_chart_login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css" >
body { width:780px; margin:0px auto;}
form { width:400px; margin:0px auto;}
h3 { margin:10px; padding:10px; text-align:center;}
p.tc { text-align:center; }
</style>
</head>
<body>
<form id="form1" runat="server" defaultbutton="Button1" defaultfocus="txt_id">
<div>
<h3>聊天室登入</h3>
<div>
<p class="tc">
<span >使用者名稱:</span>
<asp:TextBox ID="txt_id" runat="server"></asp:TextBox> </p>
<p class="tc">
<asp:Button ID="Button1" runat="server" Text="登入聊天室" onclick="Button1_Click" />
</p>
</div>
</div>
</form>
</body>
</html>
login.aspx.cs代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Sample_chart_login : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //記錄session: 目前使用者名 //跳轉至聊天室頁面 if (txt_id.Text != "") { Session["s_id"] = txt_id.Text; Server.Transfer("Sample_chat_room.aspx"); } }}
room.aspx代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_chat_room.aspx.cs" Inherits="Sample_chat_room" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css" >
body { width:780px; margin:0px auto;}
h3 { margin:10px; padding:10px; text-align:center;}
p.tc { text-align:center; }
#pnl_chat
{ margin:10px; padding:10px;
border:1px solid #dadada;
height:300px;
}
#div_ctls
{ margin:10px; padding:10px;
border:1px solid #dadade;
}
</style>
</head>
<body >
<form id="form1" runat="server" defaultbutton="Button1" defaultfocus="txt_word">
<div>
<h3>簡易聊天室</h3>
<asp:Panel ID="pnl_chat" runat="server" ScrollBars="Vertical">
</asp:Panel>
<div id="div_ctls">
<p>
<asp:TextBox ID="txt_word" runat="server" Width="400"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="發送" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="重新整理聊天記錄" />
<asp:Button ID="Button4" runat="server" Text="清空" onclick="Button4_Click" />
<asp:Button ID="Button3" runat="server" Text="退出聊天" onclick="Button3_Click" />
</p>
<p>
<span>選擇我的顏色:</span>
<asp:DropDownList ID="ddl_color" runat="server">
<asp:ListItem Value="#666666">預設</asp:ListItem>
<asp:ListItem Value="red">紅色</asp:ListItem>
<asp:ListItem Value="green">綠色</asp:ListItem>
<asp:ListItem Value="blue">藍色</asp:ListItem>
</asp:DropDownList>
</p>
</div>
</div>
</form>
</body>
</html>
room.aspx.cs代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Sample_chat_room : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { //檢測session是否存在,如果沒有session值,返回登入頁面 if (Session["s_id"] == "" || Session["s_id"] == null) { Response.Redirect("Sample_chat_login.aspx"); } //如果還沒有Application["chat"]則建立,如果有則寫入panel if (Application["chat"] != null) { pnl_chat.Controls.Add((Panel)Application["chat"]); } else { Panel _pnl = new Panel(); Application["chat"] = _pnl; } } protected void Button1_Click(object sender, EventArgs e) { if(txt_word.Text !="") { // 注意:實際應用中,文字框是否為空白,都應在前台進行檢測; Label lab_name = new Label(); lab_name.Text = Session["s_id"].ToString() + "[" + DateTime.Now.ToLongTimeString() + "]:"; Label lab_word = new Label(); lab_word.Style.Add("color", ddl_color.SelectedValue); lab_word.Text = txt_word.Text; Literal br = new Literal(); br.Text = "<br/>"; Panel _apppnl = (Panel)Application["chat"]; _apppnl.Controls.AddAt(0, br); _apppnl.Controls.AddAt(0, lab_word); _apppnl.Controls.AddAt(0, lab_name); //_apppnl.Controls.Add(lab_name); //_apppnl.Controls.Add(lab_word); //_apppnl.Controls.Add(br); Application.Lock(); Application["chat"] = _apppnl; Application.UnLock(); //清空文字框 txt_word.Text = ""; pnl_chat.Controls.Add((Panel)Application["chat"]); } } protected void Button3_Click(object sender, EventArgs e) { Session.Remove("s_id"); Response.Redirect("Sample_chat_login.aspx"); } protected void Button2_Click(object sender, EventArgs e) { } protected void Button4_Click(object sender, EventArgs e) { Application.Lock(); Application.Remove("chat"); Application.UnLock(); Server.Transfer("Sample_chat_room.aspx"); }}
Session和Application實現網路線上聊天室執行個體