ASP.net(C#)實現(xiàn)簡易聊天室功能
本文實例為大家分享了ASP.net(C#)實現(xiàn)簡易聊天室功能的具體代碼,供大家參考,具體內(nèi)容如下
1.搭建框架
<html > <head> ? ? <title>聊天系統(tǒng)</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.框架涉及三個頁面
建立相應(yīng)的頁面布局:
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('用戶名已經(jīng)存在')</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 ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
數(shù)據(jù)庫SqlParameter 的插入操作,防止sql注入的實現(xiàn)代碼
今天學(xué)習(xí)了一下SqlParameter的用法,原來這么寫是為了防止sql注入,破壞數(shù)據(jù)庫的。并自己動手連接了數(shù)據(jù)庫。2013-04-04ASP.NET MVC小結(jié)之基礎(chǔ)篇(二)
本文續(xù)上篇文章,還是介紹些asp.net mvc相關(guān)的基礎(chǔ)知識,非常的詳細(xì),新手朋友們看看,高手們略過吧2014-11-11在WinForm和WPF中使用GMap.Net地圖插件簡單教程
GMap.NET是一個強大、免費、跨平臺、開源的.NET控件,它在Windows Forms 和WPF環(huán)境中能夠使用來自Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac等地圖,下面看一下使用方法2013-12-12ABP(現(xiàn)代ASP.NET樣板開發(fā)框架)系列之二、ABP入門教程詳解
ABP是為新的現(xiàn)代Web應(yīng)用程序使用最佳實踐和使用最流行工具的一個起點??勺鳛橐话阌猛镜膽?yīng)用程序的基礎(chǔ)框架或項目模板。接下來通過本文給大家詳細(xì)介紹ABP入門教程,感興趣的朋友一起看看吧2017-10-10GridView分頁的實現(xiàn)以及自定義分頁樣式功能實例
本文為大家詳細(xì)介紹下GridView實現(xiàn)分頁并自定義的分頁樣式,具體示例代碼如下,有想學(xué)習(xí)的朋友可以參考下哈,希望對大家有所幫助2013-07-07visual studio 2017企業(yè)版本安裝(附序列號)
這篇文章主要介紹了visual studio 2017企業(yè)版本安裝,文末為大家分享了序列號,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03基于ASP.NET Core數(shù)據(jù)保護生成驗證token示例
本篇文章主要介紹了基于ASP.NET Core數(shù)據(jù)保護生成驗證token,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02