今天就跟大家聊聊有关如何在ASP.NET中利用应用程序与会话对象编写一个聊天室小程序,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
ASP.NET中有两个重要的对象,一个是应用程序对象,一个是会话对象。
应用程序:记录应用程序参数的对象,该对象用于共享应用程序级信息。
会话:记录浏览器端的变量对象,用来存储跨网页程序程序的变量或者对象只
说实话,写了快一年的ASP.NET应用程序对象还真没怎么用过。看了看书,根据这两个对象的特性写了一个简单的聊天室程序。真的是非常的简陋。
我的思路是,有两个页面默认页和聊天室页,页面布局如图:
默认页:
聊天室页:
,,,,,,,}
,,,,,,,保护无效Session_Start(对象发送方,EventArgs e)
,,,,,,,{
,,,,,,,,,,,Application.Lock ();
,,,,,,,,,,,应用程序(“count")=(int)应用程序(“count") + 1,
,,,,,,,,,,,Application.UnLock ();
,,,,,,,}
,,,,,,,保护无效Session_End(对象发送方,EventArgs e)
,,,,,,,{
,,,,,,,,,,,Application.Lock ();
,,,,,,,,,,,应用程序(“count")=(int)应用程序(“count") - 1,
,,,,,,,,,,,Application.UnLock ();
,,,,,,,}
违约。aspx:
,protected void button_login_Click (object ,发送方,EventArgs e) ,,,{ ,,,,if (! string.IsNullOrEmpty (textbox_nickname.Text)) ,,,,,,{ ,,,,,,,会话(“nickname"),=, textbox_nickname.Text; ,,,,,,} ,,,,Response.Redirect (“ChatRoom.aspx"); ,,,}
ChatRoom.aspx:
,,,,保护无效employee(对象发送方,EventArgs e)
,,,,,,,{
,,,,,,,,,,,label_count。文本=暗鼻霸谙呷耸?应用程序(“count") .ToString() +“人“;
,,,,,,,,,,,如果(! IsPostBack)
,,,,,,,,,,,{
,,,,,,,,,,,,,,,如果会话(“nickname") !=null)
,,,,,,,,,,,,,,,{
,,,,,,,,,,,,,,,,,,,Application.Add(会话(“nickname") .ToString(),会话[“nickname"]);
,,,,,,,,,,,,,,,}
,,,,,,,,,,,,,,,textbox_records。文本=应用程序(“content") .ToString ();
,,,,,,,,,,,}
,,,,,,,,,,,listbox_usernames.Items.Clear ();
,,,,,,,,,,,foreach (string str Application.Contents)
,,,,,,,,,,,{
,,,,,,,,,,,,,,,如果(! str.Equals (“content"),,! str.Equals (“count"),,! str.Equals (“name")
,,,,,,,,,,,,,,,null