Session and application realization of online chat room instances

Source: Internet
Author: User

The Login.aspx code is as follows:

<%@ 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 ">


<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>

<body>
<form id= "Form1" runat= "Server" defaultbutton= "Button1" defaultfocus= "txt_id" >
<div>


<div>
<p class= "TC" >
<span > User name:</span>
<asp:textbox id= "txt_id" runat= "Server" ></asp:TextBox> </p>

<p class= "TC" >
<asp:button id= "Button1" runat= "server" text= "Login chat room" onclick= "Button1_Click"/>
</p>
</div>

</div>
</form>
</body>

The Login.aspx.cs code is as follows:

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)    {        // Record session: Current user name        //Jump to chat Room page        if (txt_id. Text = "") {            session["s_id"] = txt_id. Text;            Server.Transfer ("sample_chat_room.aspx");}}}    

The room.aspx code is as follows:

<%@ 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 ">


<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>



<body >
<form id= "Form1" runat= "Server" defaultbutton= "Button1" defaultfocus= "Txt_word" >
<div>

<asp:panel id= "Pnl_chat" runat= "Server" scrollbars= "Vertical" >
</asp:Panel>

<div id= "Div_ctls" >
<p>
<asp:textbox id= "Txt_word" runat= "Server" width= "></asp:TextBox>"
<asp:button id= "Button1" runat= "Server" text= "send" onclick= "Button1_Click"/>
&nbsp;
<asp:button id= "Button2" runat= "Server" text= "Refresh Chat history"/>
&nbsp;
<asp:button id= "Button4" runat= "server" text= "Empty" onclick= "Button4_Click"/>
&nbsp;
<asp:button id= "Button3" runat= "Server" text= "Exit chat" onclick= "Button3_Click"/>
</p>

<p>
<span> Choose my Color:</span>

<asp:dropdownlist id= "Ddl_color" runat= "Server" >
<asp:listitem value= "#666666" > Default </asp:ListItem>
<asp:listitem value= "Red" > Red </asp:ListItem>
<asp:listitem value= "green" > Greens </asp:ListItem>
<asp:listitem value= "Blue" > Blue </asp:ListItem>
</asp:DropDownList>


</p>
</div>


</div>
</form>
</body>

The Room.aspx.cs code is as follows:

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 s Ender, EventArgs E) {//detects if the session exists, if there is no session value, return to the login page if (session["s_id"] = = "" | |        session["s_id"] = = null) {Response.Redirect ("sample_chat_login.aspx");                }//If there is no application["chat"] then create, if any, write to 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 = "") {//Note: In practice, the text box is empty and should be detected in the foreground; 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 (); Clears the text box 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 and application realization of online chat room instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.