SessionA和pplication网上聊天室的网络范例

SessionA和pplication网上聊天室的网络范例

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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");
    }
}

SessionA和pplication网上聊天室的网络范例
SessionA和pplication网上聊天室的网络范例

     

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117679.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月2日 上午6:00
下一篇 2022年1月2日 上午6:00


相关推荐

  • 错误端口已被占用1080_端口已打开 错误1231

    错误端口已被占用1080_端口已打开 错误1231更新记录版本时间修订内容1.02018-3-28增加了方案二问题的提出【实验环境】:Win764bit也许你会碰到以下错误:本文给出2种解决方案。方案一既然说端口已被占用,那就需要找出是哪个程序占用了1080端口。打开cmd.exe,输入命令:netstat-aon|findstr"1080"最后一列就是PID了,…

    2026年1月15日
    6
  • 2021年最好用&完全免费的图片压缩网站、软件推荐(包括GIF)

    2021年最好用&完全免费的图片压缩网站、软件推荐(包括GIF)最近我有遇到一个很奇怪的问题因为我不是转用AppleMusic本地化听歌了????所以很多歌的歌曲信息都是我自己补充的,当然也包括封面但我在用iTunes把歌传到iPhone上来听的时候,有首歌的封面怎么都同步不过来我来回同步了几遍,还重新连接了几次,甚至换回了有线来同步,这个封面始终都还是同步不上…我就一直奇了怪了直到我想重新编辑一下封面,重新添加,我才发现…好家伙,一张封面竟然有18M!?比我MP3本身都要大了,难怪我添加不上呢完全被它小小的外表给欺骗了我后来把图片

    2022年6月18日
    30
  • linux制作img镜像文件_linux用命令打开浏览器

    linux制作img镜像文件_linux用命令打开浏览器linuxrootfs.img的制作cramfs是只读压缩的文件系统,文件系统类型可以是ext2,ext3,什么的, cramfs和romfs只是一个文件系统类型,ramdisk相当于一块硬盘空间,可以理解为在内存中虚拟出一块硬盘来,所以它上面就可以有你linux支持的各种文件系统什么的。所以你问的,它和romfs和cramfs确实不是一个层次的概念。^-^恭喜你,你答

    2026年4月15日
    5
  • 打造7 * 24小时的AI员工-本地安装OpenClaw操作步骤

    打造7 * 24小时的AI员工-本地安装OpenClaw操作步骤

    2026年3月13日
    3
  • js获取应用服务器时间,JavaScript获取服务器端时间的方法

    js获取应用服务器时间,JavaScript获取服务器端时间的方法用 js 做时间校正 获取本机时间 是存在 bug 的 使用 js 也可获取到服务器时间 原理是使用 ajax 请求 返回的头部信息就含有服务器端的时间信息 获取到就可以了 以下 1 依赖 jQuery 代码 functiongetS returnnewDat ajax async false getResponseH Date 以上函数返回的就是一个 D

    2026年3月16日
    2
  • 海量数据存储技术与解决方案[通俗易懂]

    海量数据存储难点:数据量过大,数据中什么情况都可能存在;软硬件要求高,系统资源占用率高;要求很高的处理方法和技巧。海量数据存储处理经验:一、选用优秀的数据库工具    现在的数据库工具厂家比较多,对海量数据的处理对所使用的数据库工具要求比较高,一般使用Oracle或者DB2,微软公司最近发布的SQLServer2005性能也不错。另外在BI领域:数据库,数据仓库,多维数据库,数据挖

    2022年4月14日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号