Session和Application實現網路線上聊天室執行個體

來源:互聯網
上載者:User

標籤: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" />
        &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代碼如下:

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實現網路線上聊天室執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.