1. 程式人生 > >Session和Application實現網路線上聊天室例項

Session和Application實現網路線上聊天室例項

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程式碼如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class Sample_chart_login : System.Web.UI.Page  
  8. {  
  9.     protectedvoid Page_Load(object sender, EventArgs e)  
  10.     {  
  11.     }  
  12.     protectedvoid Button1_Click(object sender, EventArgs e)  
  13.     {  
  14.         //記錄session: 當前使用者名稱
  15.         //跳轉至聊天室頁面
  16.         if (txt_id.Text != "") {  
  17.             Session["s_id"] = txt_id.Text;  
  18.             Server.Transfer("Sample_chat_room.aspx");  
  19.         }  
  20.     }  
  21. }  

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" />
        &nbsp;
            <asp:Button ID="Button2" runat="server" Text="重新整理聊天記錄"  />
         &nbsp;
            <asp:Button ID="Button4" runat="server" Text="清空" onclick="Button4_Click"  />
        &nbsp;
            <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程式碼如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class Sample_chat_room : System.Web.UI.Page  
  8. {  
  9.     protectedvoid Page_Load(object sender, EventArgs e)  
  10.     {  
  11.         //檢測session是否存在,如果沒有session值,返回登入頁面
  12.         if (Session["s_id"] == "" || Session["s_id"] == null) {  
  13.             Response.Redirect("Sample_chat_login.aspx");  
  14.         }  
  15.             //如果還沒有Application["chat"]則建立,如果有則寫入panel
  16.             if (Application["chat"] != null)  
  17.             {  
  18.                 pnl_chat.Controls.Add((Panel)Application["chat"]);  
  19.             }  
  20.             else
  21.             {  
  22.                 Panel _pnl = new Panel();  
  23.                 Application["chat"] = _pnl;  
  24.             }  
  25.     }  
  26.     protectedvoid Button1_Click(object sender, EventArgs e)  
  27.     {  
  28.         if(txt_word.Text !="") { // 注意:實際應用中,文字框是否為空,都應在前臺進行檢測;
  29.         Label lab_name = new Label();  
  30.         lab_name.Text = Session["s_id"].ToString() + "[" + DateTime.Now.ToLongTimeString() + "]:";  
  31.         Label lab_word = new Label();  
  32.         lab_word.Style.Add("color", ddl_color.SelectedValue);  
  33.         lab_word.Text = txt_word.Text;  
  34.         Literal br = new Literal();  
  35.         br.Text = "<br/>";  
  36.         Panel _apppnl = (Panel)Application["chat"];  
  37.         _apppnl.Controls.AddAt(0, br);  
  38.         _apppnl.Controls.AddAt(0, lab_word);  
  39.         _apppnl.Controls.AddAt(0, lab_name);  
  40.         //_apppnl.Controls.Add(lab_name);
  41.         //_apppnl.Controls.Add(lab_word);
  42.         //_apppnl.Controls.Add(br);
  43.         Application.Lock();  
  44.         Application["chat"] = _apppnl;  
  45.         Application.UnLock();  
  46.         //清空文字框
  47.         txt_word.Text = "";  
  48.         pnl_chat.Controls.Add((Panel)Application["chat"]);  
  49.         }  
  50.     }  
  51.     protectedvoid Button3_Click(object sender, EventArgs e)  
  52.     {  
  53.         Session.Remove("s_id");  
  54.         Response.Redirect("Sample_chat_login.aspx");  
  55.     }  
  56.     protectedvoid Button2_Click(object sender, EventArgs e)  
  57.     {  
  58.     }  
  59.     protectedvoid Button4_Click(object sender, EventArgs e)  
  60.     {  
  61.         Application.Lock();  
  62.         Application.Remove("chat");  
  63.         Application.UnLock();  
  64.         Server.Transfer("Sample_chat_room.aspx");  
  65.     }  
  66. }  

轉自:http://blog.csdn.net/yayun0516/article/details/42024223