初识 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文件,具体代码如下:

问人数的网站默认主页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();
        }
    }
}

下面是Context.aspx页面用来显示用户聊天信息,页面如下:

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();
        }
    }
}

然后就是Login.aspx登录页面,该页面是实现判断用户是否登录,没有做的像正规登录那么严谨,页面如下:

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>");
        }
    }
}

下面是我们的List.aspx页面用于显示在线用户,页面如下:

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();
        }
    }
}

现在把Login.aspx设为默认启动页面运行程序效果如下:

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值