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

这篇文章主要为大家详细介绍了ASP.net实现简易聊天室功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

1.搭建框架

      聊天系统                                                                                            

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("");             return;         }         if (LoginPwd.Text!= "123456")         {undefined             Response.Write("");             return;         }         if (!IfLonined())         {undefined             Response.Write("");             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 = "" + Session["username"].ToString() + "说:";         message += Message.Text;         message += "(" + DateTime.Now.ToString() + ")";         message += "
";         Application.Lock();         if (chk.Checked)             Application["chatcontent"] = (string)Application["chatcontent"] + message + "" + "";         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     } }

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

以上就是ASP.net(C#)实现简易聊天室功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程