ASP.net(C#)实现简易聊天室功能

 更新时间:2022年02月12日 08:50:42   作者:「已注销」  
这篇文章主要为大家详细介绍了ASP.net实现简易聊天室功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ASP.net(C#)实现简易聊天室功能的具体代码,供大家参考,具体内容如下

1.搭建框架

<html >
<head>
    <title>聊天系统</title>
</head>
    <frameset rows="80%,20%" >
          <frameset cols="20%,80%">
          <frame src="Register.aspx" />
               <frame src="main.aspx" /> 
                     </frameset>
               <frame src="login.aspx"/>

      </frameset>

<body >
 
</body>
</html>

2.框架涉及三个页面  

建立相应的页面布局:

1.login.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class login : System.Web.UI.Page
{undefined
    protected void Page_Load(object sender, EventArgs e)
    {undefined

    }
    protected void LoginBtn_Click(object sender, EventArgs e)
    {undefined
        if (LoginID.Text.Trim() == string.Empty)
        {undefined
            Response.Write("<script>alert('请输入用户名!')</script>");
            return;
        }
        if (LoginPwd.Text!= "123456")
        {undefined
            Response.Write("<script>alert('密码不正确,请重新输入')</script>");
            return;
        }
        if (!IfLonined())
        {undefined
            Response.Write("<script>alert('用户名已经存在')</script>");
            return;
        }
        Session["username"] = LoginID.Text;
        if (Application["user"] == null)
        {undefined
            Application["user"] = Session["username"];
        }
        else {undefined
            Application["user"] += "," + Session["username"];

        }
        Response.Redirect("send.aspx");

    }
    protected bool IfLonined()
    {undefined
        Application.Lock();
        string users;
        string[]user;
        if (Application["user"]!=null)
        {undefined
            users = Application["user"].ToString();
            user = users.Split(',');
            foreach(string s in user)
            {undefined
                if(s==LoginID.Text.Trim().ToString())
                {undefined
                    return false;
                }
            }
        }
        Application.UnLock();
        return true;
    }
    protected void LoginPWD_TextChanged(object sender, EventArgs e)
    {undefined

}
}

2.Register.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{undefined
    protected ArrayList ItemList = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {undefined
        Response.AddHeader("Refresh", "1");
        Application.Lock();
        string users;
        string[] user;
        if (Application["user"]!=null)
        {undefined
            users = Application["user"].ToString();
            user = users.Split(',');
            for(int i=user.Length-1;i>=0;i--)
            {undefined
                ItemList.Add(user[i].ToString());

            }
            UserList.DataSource = ItemList;
            UserList.DataBind();
        }
        Application.UnLock();
    }
    protected void UserList_SelectedIndexChanged(object sender, EventArgs e)
    {undefined

    }
}

3.send.asp

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class send : System.Web.UI.Page
{undefined
    protected void Page_Load(object sender, EventArgs e)
    {undefined
        if (Session["username"] != null)
        {undefined
            Username.Text = Session["username"].ToString() + "说:";
        }
        else
        {undefined
            Response.Redirect("login.aspx");
        }


    }
    protected void SendBtn_Click(object sender, EventArgs e)
    {undefined
        string message;
        message = "<font color='blue'>" + Session["username"].ToString() + "</font>说:";
        message += Message.Text;
        message += "(<i>" + DateTime.Now.ToString() + "</i>)";
        message += "<br>";
        Application.Lock();
        if (chk.Checked)
            Application["chatcontent"] = (string)Application["chatcontent"] + message + "<img src=image/00.gif>" + "<img src=image/01.gif>";
        else
            Application["chatcontent"] = (string)Application["chatcontent"] + message;
      
        Application.UnLock();
        Message.Text = null;
    }
    protected void LoginBtn_Click(object sender, EventArgs e)
    {undefined
        Response.Redirect("login.aspx");
    }
    protected void LoginOutBtn_Click(object sender, EventArgs e)
    {undefined
        Application.Lock();
        if (Application["user"] != null)
        {undefined
            string users;
            string[] user;
            users = Application["user"].ToString();
            Application["user"] = null;
            user = users.Split(',');
            foreach (string s in user)
            {undefined
                if (s != Session["username"].ToString())
                {undefined
                    if (Application["user"] == null)
                    {undefined
                        Application["user"] = s;
                    }

                    else
                    {undefined
                        Application["uesr"] = Application["user"] + "," + s;
                    }
                }
            }
        }
        if (Session["username"] != null)
        {undefined
            Session["username"] = null;
        }
        Application.UnLock();
        Response.Redirect("login.aspx");


    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {undefined

    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • .NET Core中Object Pool的多种用法详解

    .NET Core中Object Pool的多种用法详解

    本文通过实例代码给大家介绍了.NET Core中Object Pool的简单使用 ,大概给大家提供了四种方法,每种方法都很经典,需要的朋友可以参考下
    2018-09-09
  • Asp.net基于ajax和jquery-ui实现进度条

    Asp.net基于ajax和jquery-ui实现进度条

    这篇文章主要介绍了Asp.net基于ajax和jquery-ui实现进度条,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • asp.net中如何批量导出access某表内容到word文档

    asp.net中如何批量导出access某表内容到word文档

    最近有项目需求是这样的,需要将某表中的每一条记录中的某些内容导出在一个word文档中。下面小编就把我的解决办法分享给大家,供大家参考
    2015-10-10
  • ASP.NET Core中间件设置教程(7)

    ASP.NET Core中间件设置教程(7)

    这篇文章主要为大家详细介绍了ASP.NET Core中间件的设置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • ASP.NET MVC实现多个按钮提交的方法

    ASP.NET MVC实现多个按钮提交的方法

    这篇文章主要为大家详细介绍了ASP.NET MVC实现多个按钮提交的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 网页(aspx)与用户控件(ascx)交互逻辑处理实现

    网页(aspx)与用户控件(ascx)交互逻辑处理实现

    为了以后好维护,把几个页面(ASPX)相同的部分抽取放在一个用户控件(ASCX)上,现在把逻辑分享下,感兴趣的各位可以参考下哈
    2013-03-03
  • .Net Core 集成 Kafka的步骤

    .Net Core 集成 Kafka的步骤

    这篇文章主要介绍了.Net Core 集成 Kafka的实现步骤,帮助大家更好的理解和学习使用.net技术,感兴趣的朋友可以了解下
    2021-04-04
  • asp.net Web Services上传和下载文件(完整代码)

    asp.net Web Services上传和下载文件(完整代码)

    随着Internet技术的发展和跨平台需求的日益增加,Web Services的应用越来越广,我们不但需要通过Web Services传递字符串信息,而且需要传递二进制文件信息。
    2008-12-12
  • ASP.NET MVC分页问题解决

    ASP.NET MVC分页问题解决

    这篇文章主要为大家详细介绍了ASP.NET MVC分页问题的解决方法,Ajax.Pager分页的使用注意事项,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • ASP.NET AJAX 4.0的模版编程(Template Programming)介绍

    ASP.NET AJAX 4.0的模版编程(Template Programming)介绍

    不过当我评估ASP.NET AJAX 4.0的时候,我确实被它的特征给震住了。新的特征完全专注于浏览器技术,比如XHTML和javascript。 我非常钦佩ASP.NET AJAX小组。
    2009-07-07

最新评论