初識 Asp.Net內置對象之Application對象


Application對象

     Applocation對象用於共享應用程序級信息,即多個用戶可以共享一個Applocation對象。

     用戶在請求Asp.Net文件時,將啟動應用程序並且創建Application對象。一旦Application對象被創建,就可以共享和管理整個應用程序的信息。在應用程序關閉之前,Application對象一直存在,所以Application對象是用於啟動和管理ASP.NET應用程序的主要對象。

Application對象常用集合

集合名  
Contents 用於訪問應用程序狀態集合中的對象名
StaticObjects 確定某對象指定屬性的值或遍歷集合,並且檢索所有靜態對象的屬性

 

 

 

 

Application對象常用屬性

屬性  
AllKeys 返回全部Application對象變量名到一個字符串數組中
Count 獲取Application對象的數量
Item 允許使用索引或Application變量名稱傳回內容值

 

 

 

 

Application對象常用方法

方法  
Add 新增一個Application對象變量
Clear 清除全部Application對象變量
Lock 鎖定全部Application對象變量
Remove 使用變量名移除一個Application對象變量
RemoveAll 移除全部Application對象變量
Set 使用變量名稱更新一個Application對象變量的內容
UnLock 解除鎖定的Application對象變量

 

 

 

 

 

 

 

1.用Application實驗一個訪問計數器

   主要用來記錄應用程序曾經被訪問的次數的組件。主要通過Application對象和Session對象來實現,我們在昨天的項目添加一個Global.asax文件,具體代碼如下:

 對Global.asax文件經行配置后,我們將訪問人數的網站默認主頁Application.aspx,在Application.aspx添加一個Lable控件,用雨顯示訪問人數。代碼如下:

,寫好,運行程序會看到以下效果:

2.利用Application對象制作一個簡單的聊天會話

     准備工作:新建立一個Web空網站,一次加入這幾個頁面和全局程序集文件進來,Content.aspx頁面用來顯示用戶聊天信息,Default.aspx頁面為聊天室主頁面,Login.aspx用來登錄用戶。哦對我們還需要一個List.aspx頁面來顯示在線人數。Global.asax用來初始化Application對對象值。Global.asax初始代碼如下:

在聊天室主頁中,單擊發送按鈕時,程序調用Application對象的Lock方法對所有的Application對象經行鎖定,然后判斷當前聊天信息的記錄數是否大於15,如果大於15,則清空聊天記錄,並且重新加載用戶聊天記錄,否則聊天內容,用戶,發送消息以及時間都會被保存在Application對象中。現在看看我們聊天主頁面Default.aspx頁面簡單布局如下:

 

大致畫下頁面用iframe嵌套其他倆個頁面進來(上面一個右邊iframe應該用來顯示在線人數,這里URL路徑沒引用好是因為我們還沒來得及加List.aspx頁面實驗的時候最好先加上這個頁面),Default.aspx.cs代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            int P_count = Convert.ToInt32(Application["count"]);
            Application.Lock();
            if (P_count == 0 || P_count>20)
            {
                P_count = 0;
                Application["Chats"]=Session["userName"].ToString()+"說:"+ txtMessage.Text.Trim()+"("+DateTime.Now.ToString()+")";
            }else
            {
                Application["Chats"] = Application["Chats"].ToString() + "," + Session["userName"].ToString() + "說:" + txtMessage.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
            }
            P_count += 1;
            Application["current"] = P_count;
            Application.UnLock();
        }
    }
}
View Code

 下面是Context.aspx頁面用來顯示用戶聊天信息,頁面如下:

Content.aspx.cs的代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyApplication
{
    public partial class Content : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int P_int_current = Convert.ToInt32(Application["Current"]);
            Application.Lock();
            string P_str_chats = Application["Chats"].ToString();
            string[] P_str_chat = P_str_chats.Split(',');
            for (int i = P_str_chat.Length - 1; i >= 0; i--)
            {
                if (P_int_current == 0)
                {
                    this.txtContex.Text = P_str_chat[i].ToString();
                }
                else
                {
                    txtContex.Text = txtContex.Text + "\n" + P_str_chat[i].ToString();
                }
            }
            Application.UnLock();
        }
    }
}
View Code

然后就是Login.aspx登錄頁面,該頁面是實現判斷用戶是否登錄,沒有做的像正規登錄那么嚴謹,頁面如下:

Login.aspx.cs代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyApplication
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int P_int_judge = 0;
            P_int_judge = Convert.ToInt32(Request["value"]);
            if (!IsPostBack)
            {
                if (P_int_judge == 1)
                    Response.Write("<script>alert('該用戶已經登錄!')</script>");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Application.Lock();
            int P_int_num;     //在線人數
            string P_str_name; //登錄用戶
            string P_str_names;  //已在線的用戶名
            string[] P_str_user; //用戶在線數組
            P_int_num = int.Parse(Application["userNum"].ToString());
            if (TextBox1.Text == "")
            {
                Response.Write("<script>alert('用戶名不能為空')</script>");
                TextBox1.Focus();
            }
            else
            {
                P_str_name = TextBox1.Text.Trim();
                P_str_names = Application["user"].ToString();
                P_str_user = P_str_names.Split(',');
                for (int i = 0; i <= P_int_num - 1; i++)
                {
                    if (P_str_name == P_str_user[i].Trim())
                    {
                        int P_int_judge = 1;
                        Response.Redirect("Login.aspx?value=" + P_int_judge);
                    }
                }
                if (P_int_num == 0)
                    Application["user"] = P_str_name.ToString();
                else
                    Application["user"] = Application["user"] + "," + P_str_name.ToString();
                P_int_num += 1;
                Application["userNum"] = P_int_num;
                Session["userName"] = TextBox1.Text.Trim();
                Application.UnLock();
                Response.Redirect("Default.aspx");
            }
        }
  
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.close();</script>");
        }
    }
}
View Code

下面是我們的List.aspx頁面用於顯示在線用戶,頁面如下:

List.aspx.cs代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyApplication
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList ItemList = new ArrayList();
            Application.Lock();
            string P_str_names;       //已在線的用戶名
            string[] P_str_user;        //用戶在線數組
            int P_int_num = Convert.ToInt32(Application["userNum"]);
            P_str_names = Application["user"].ToString();
            P_str_user = P_str_names.Split(',');
            for (int i = (P_int_num - 1); i >= 0; i--)
            {
                if (P_str_user[i].ToString() != "")
                    ItemList.Add(P_str_user[i].ToString());
            }
            lbList.DataSource = ItemList;
            lbList.DataBind();
            Application.UnLock();
        }
    }
}
View Code

現在把Login.aspx設為默認啟動頁面運行程序效果如下:

這里沒有做發出消息后應該將文本框內容情況的操作,自己實驗的時候可以加上那么一句。。。。

關於Application就先了解這么一點吧,希望各位朋友不惜指教,大家共同學習。。。。。

 實例源碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM