Application对象

一、Application对象

1.定义:Application对象用于共享应用程序级信息,即多个用户共享一个Application对象。

2.常用集合

Application对象的常用集合
集合 说明
Contents 用于访问应用程序状态集合中的对象名
StaticObject 确定某对象指定属性的值或遍历集合,并检索所有静态对象的属性

3.常用属性

Application对象的常用属性
属性 说明
AllKeys 返回全部Application对象变量名到一个字符串数组中
Count 获取Application对象变量的数量
Item 允许使用索引或Application变量名称传回内容值

4.常用方法

Application对象的常用方法
方法 说明
Add 新增一个Application对象变量
Clear 清除全部Application对象变量
Lock 锁定全部Application对象变量
Remove 使用变量名称移除一个Application对象变量
RemoveAll 移除全部Application对象变量
Set 使用变量名更新一个Application对象变量的内容
UnLock 解除锁定的Application对象变量

5.实例

(1)访问计数器

前台代码

    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>&nbsp;</div>
    </form>

后台代码

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "您是该网站的第" + Application["count"].ToString() + "个访问者";
    }

Application对象的设置代码

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        Application["count"] = 0;
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
        
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码
        Application.Lock();
        Application["count"] = (int)Application["count"] + 1;
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。
        Application.Lock();
        Application["count"] = (int)Application["count"] - 1;
        Application.UnLock();
    }
       
</script>

结果

(2)聊天室

登陆页面代码

    <form id="form1" runat="server">
    <div>
        <table align="center" border="1" cellpadding="0" cellspacing="0" style="width: 199px;
            height: 100px">
            <tr>
                <td colspan="2" style="font-weight: bold; font-size: 16pt; color: #ffffff; background-color: #54a4ff;
                    text-align: center">
                    登录</td>
            </tr>
            <tr>
                <td style="font-size: 9pt; background-color: #f7f6f3; text-align: right">
                    用户名:</td>
                <td style="background-color: #f7f6f3; text-align: center">
                    <asp:TextBox ID="TextBox1" runat="server" Width="113px"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="background-color: #f7f6f3">
                </td>
                <td style="background-color: #f7f6f3; text-align: center">
                    <asp:Button ID="btnLogin" runat="server" Font-Size="9pt" OnClick="btnLogin_Click"
                        Text="登录" />
                    &nbsp;
                    <asp:Button ID="btnExit" runat="server" Font-Size="9pt" OnClick="btnExit_Click" Text="退出" /></td>
            </tr>
        </table>
    
    </div>
    </form>
    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>");
    }

主页面代码

    <form id="form1" runat="server">
    <div style="text-align: center">
        <table style="width: 603px; height: 442px" border="1" cellpadding="0" cellspacing="0">
            <tr>
                <td colspan="2" style="height: 51px; font-size: 16pt; color: #ffffff; background-color: #54a4ff;">
                    聊天室</td>
            </tr>
            <tr>
                <td style="width: 404px; height: 18px">
                </td>
                <td align="left" style="width: 200px; height: 18px; font-size: 9pt; border-right-style: none; border-left-style: none;">
                    用户列表:</td>
            </tr>
            <tr>
                <td style="width: 404px; height: 354px;">
                <iframe id="Iframe1" src="Content.aspx" runat="server" scrolling="no" noresize="noResize" frameborder="0" style="width: 400px; height: 350px"></iframe>
                </td>
                <td style="width: 200px; height: 354px;">
                <iframe id="Iframe2" src="List.aspx" runat="server" frameborder="no" noresize="noResize" scrolling="no" style="width: 200px; height: 350px"></iframe>
                </td>
            </tr>
            <tr>
                <td style="width: 400px" align="left">
                    <asp:TextBox ID="txtMessage" runat="server" Width="316px"></asp:TextBox><asp:Button ID="btnSend" runat="server" Text="发送" OnClick="btnSend_Click" /><asp:Button ID="btnExit" runat="server" Text="退出" OnClick="btnExit_Click" /></td>
                <td style="width: 200px">
                </td>
            </tr>
        </table>
    
    </div>
    </form>
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        int P_int_current = Convert.ToInt32(Application["current"]);
        Application.Lock();
        if (P_int_current == 0 || P_int_current > 20)
        {
            P_int_current = 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_int_current += 1;
        Application["current"] = P_int_current;
        Application.UnLock();
    }
    protected void btnExit_Click(object sender, EventArgs e)
    { 
        
        Application.Lock();
        string P_str_userName = Application["user"].ToString();
        Application["user"] = P_str_userName.Replace(Session["userName"].ToString(), "");
        Application.UnLock();
        Response.Write("<script>window.opener=null;window.close();</script>");
    }

用户聊天信息页面代码

    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtContent" runat="server" Height="345px" TextMode="MultiLine" Width="380px"></asp:TextBox></div>
    </form>
    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)
            {
                txtContent.Text = P_str_chat[i].ToString();
            }
            else
            {
                txtContent.Text = txtContent.Text + "\n" + P_str_chat[i].ToString();
            }
        }
        Application.UnLock();
    }

在线用户页面代码

    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="lbList" runat="server" Height="345px" Width="180px"></asp:ListBox></div>
    </form>
    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();
    }

Application对象设置的代码

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        //建立用户列表
        string user = "";//用户列表
        Application["user"] = user;
        Application["userNum"] = 0;
        string chats = "";//聊天记录  
        Application["chats"] = chats;
        //当前的聊天记录数
        Application["current"] = 0;

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
        Application["user"] = "";
        Application["chats"] = "";

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。

    }
       
</script>

结果

登录页面

聊天页面

聊天信息页面

在线用户页面

 

posted on 2016-07-23 09:54  bosamvs  阅读(2853)  评论(1编辑  收藏  举报

导航