AI智能
改变未来

asp.net做一个简易的聊天室

要求:

结果:

关键代码:
Default.aspx

<form id=\"form1\" runat=\"server\"><div><asp:Label ID=\"username_label\" runat=\"server\" Text=\"用户名:\" Width=\"80px\"></asp:Label><asp:TextBox ID=\"username_text\" runat=\"server\" ></asp:TextBox><br /><asp:Label ID=\"password_label\" runat=\"server\" Text=\"房间密码:\" Width=\"80px\"></asp:Label><asp:TextBox ID=\"password_text\" runat=\"server\"></asp:TextBox><br /><asp:Button ID=\"login\" runat=\"server\" Text=\"登录\" OnClick=\"login_Clicked\"/></div></form>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e){//Application.Clear();Session[\"username\"] = username_text.Text;}protected void login_Clicked(object sender, EventArgs e){if(password_text.Text.Equals(\"2020\")){//设置聊天室密码为2020if(Application[\"user\"]==null){Application[\"user\"]=\"<hr/>\";}else{Application.Lock();Application[\"user\"] = Session[\"username\"]+\"进入聊天室</br>\"+Application[\"user\"];Application.UnLock();Response.Redirect(\"Default4.aspx\");//聊天室}}}

Default2.aspx

<script type=\"text/javascript\">setTimeout(\"location.href=\'Default2.aspx\'\",1000);//刷新时间一秒一次</script>

Default2.aspx.cs

protected void Page_Load(object sender, EventArgs e){Response.Write(Application[\"user\"].ToString());//输出用户进入聊天室if(Application[\"chat\"]==null){//聊天记录,若没有人聊天为空,否则就打印出来聊天内容Application[\"chat\"]=\"\";}else{Response.Write(Application[\"chat\"].ToString());}}

Default3.aspx

<form id=\"form1\" runat=\"server\"><div ><%Response.Write(\"发言人:\"+Session[\"username\"]); %><br /><asp:TextBox ID=\"chatbox\" runat=\"server\"></asp:TextBox><asp:Button ID=\"text_send\" runat=\"server\" Text=\"发送\" OnClick=\"text_send_Clicked\" /></div></form>

Default3.aspx.cs

protected void text_send_Clicked(object sender, EventArgs e){Application.Lock();Application[\"chat\"] = Application[\"chat\"].ToString()+ Session[\"username\"]+\":\"+chatbox.Text+\"[\"+DateTime.Now+\"]\"+\"</br>\";Application.UnLock();Response.Redirect(\"Default3.aspx\");//此处重定向是为了删除文本框中的内容}

Default4.aspx

<form id=\"form1\" runat=\"server\"><div><iframe id=\"iframe1\" src=\"Default2.aspx\" frameborder=\"0\" width=\"100%\" height=\"300px\"></iframe></br><iframe id=\"iframe2\" src=\"Default3.aspx\" frameborder=\"0\" width=\"100%\" height=\"300px\"></iframe></div></form>
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » asp.net做一个简易的聊天室