自强不息,厚德载物!身心自在,道法自然!


初识 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就先了解这么一点吧,希望各位朋友不惜指教,大家共同学习。。。。。

 实例源码下载

posted @ 2014-04-11 19:59  辉太  阅读(6152)  评论(0编辑  收藏  举报

路漫漫其修远兮,吾将上下而求索!