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 >username:</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: 当前username
//跳转至聊天室页面
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");
}
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWF5dW4wNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

版权声明:本文博客原创文章,博客,未经同意,不得转载。

SessionA和pplication网上聊天室的网络范例的更多相关文章

  1. 开源Flex Air版免费激情美女视频聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))

    开源Flex Air版免费激情美女视频聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))   Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实 ...

  2. SharedObject对象聊天室

    本博推荐文章快速导航: Sql Server2005 Transact-SQL 新兵器学习MCAD学习 代码阅读总结 ASP.NET状态管理 DB(数据库)WAPWinFormFlex,Fms aie ...

  3. 使用Servlet和JSP实现一个简单的Web聊天室系统

    1 问题描述                                                利用Java EE相关技术实现一个简单的Web聊天室系统,具体要求如下. (1)编写一个登录 ...

  4. php websocket-网页实时聊天之PHP实现websocket(ajax长轮询和websocket都可以时间网络聊天室)

    php websocket-网页实时聊天之PHP实现websocket(ajax长轮询和websocket都可以时间网络聊天室) 一.总结 1.ajax长轮询和websocket都可以时间网络聊天室 ...

  5. Python实现网络多人聊天室

    网络多人聊天室 文件结构: chatroom ├── client.py  # 客户端代码 ├── language.py  # 语言文件 ├── server.py  # 服务端代码 └── set ...

  6. Java NIO示例:多人网络聊天室

    一个多客户端聊天室,支持多客户端聊天,有如下功能: 功能1: 客户端通过Java NIO连接到服务端,支持多客户端的连接 功能2:客户端初次连接时,服务端提示输入昵称,如果昵称已经有人使用,提示重新输 ...

  7. 网络编程(学习整理)---2--(Udp)实现简单的控制台聊天室

    1.UDP协议: 总结一下,今天学习的一点知识点! UDP也是一种通信协议,常被用来与TCP协议作比较!我们知道,在发送数据包的时候使用TCP协议比UDP协议安全,那么到底安全在哪里呢?怎么理解呢! ...

  8. 网络编程(学习整理)---1--(Tcp)实现简单的控制台聊天室

    1.简单的聊天室(控制台): 功能实现: 客户端和服务端的信息交流: 2.牵扯到的知识点: 这个我大概说一下,详细后面见代码! 1) 网络通讯的三要素 1. IP 2. 端口号. 3. 协议   2) ...

  9. 网络编程TCP协议-聊天室

    网络编程TCP协议-聊天室(客户端与服务端的交互); <span style="font-size:18px;">1.客户端发数据到服务端.</span> ...

随机推荐

  1. [计算机基础]HTTP协议学习笔记

    HTTP:Hypertext transfer protocol超文本传输协议是一种详细规定了浏览器和Internet之间互相通信的规则 HTTP允许传输任意类型的数据对象,由Content-Type ...

  2. 总结文件操作函数-文件夹(三)-C语言

    获取.改变当前文件夹: 原型为: #include <unistd.h>   //头文件 char *getcwd(char *buf, size_t size); //获取当前文件夹.相 ...

  3. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

  4. Android菜鸟的成长笔记(27)——ViewPager的使用

    ViewPager是Android 3.0以上能够使用的API. 一.ViewPager能干什么? 1.微信5.0中连带滑动用ViewPager能够轻松实现. 2.实现相似于新浪微博的导航引导界面. ...

  5. poj3662(二分+最短路)

    题目连接:http://poj.org/problem?id=3662 题意:有n个节点p条无向边,现在可以选择其中的任意K条免费,则花费为除了k条边后权值最大的一个,求最小花费多少. 分析:二分枚举 ...

  6. python实现了字符串的按位异或和php中的strpad函数

    近期在写自己主动化測试,因为开发加密中用到strpad和字符串的按位异或,而python中没有这种函数和功能,所以必须自己写一套,要不自己主动化測试无法进行,所以就用python实现了一下,因为在写字 ...

  7. three.js是JavaScript编写的WebGL第 三方库

    three.js是JavaScript编写的WebGL第 三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质 ...

  8. ASP.NET 联想控件(Autocomplete)测试可用 ascx

    效果图 前台 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Auto ...

  9. URAL 1297 后缀数组:求最长回文子串

    思路:这题下午搞了然后一直WA,后面就看了Discuss,里面有个数组:ABCDEFDCBA,这个我输出ABCD,所以错了. 然后才知道自己写的后缀数组对这个回文子串有bug,然后就不知道怎么改了. ...

  10. 内存分析工具 MAT 的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,当中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...