zoukankan      html  css  js  c++  java
  • asp.net之状态管理分类

    视图状态是在页面内的
    cookie而abc.aspx?page=1&id=5;
    服务器端的状态管理
    (Application)应用程序状态,是一个全局的状态
    会话状态(session)
    数据库

    对于应用程序状态,位于应用程序根目录下(虚拟目录,和虚拟目录的状态,一个虚拟目录也可以当成一个应用程序根目录 一般是 Gloabal.asax),用户不可以浏览(它没有用户界面).不响应页面请求.
    具体的实例:
      

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;

    namespace WebApplication1
    {
        
    public class Global : System.Web.HttpApplication
        
    {

            
    protected void Application_Start(object sender, EventArgs e)
            
    {

            }


            
    protected void Application_End(object sender, EventArgs e)
            
    {

            }

        }

    }

       缺点:内存占用.同步问题(锁定问题,多人访问的时候会有锁定的过程,否则会出现死锁等问题).生命周期(直到机器发生故障<iis发生故障>)
    它的赋值:
        Application["var_name"]=name;
    取值:
       var_name=Application["var_name"];
    同步:
    Application.lock()
    Application.Unlock()

    如:做聊天室在线人数的统计 设计思路:
       网站运行,初始化统计变量;
       用户进入,统计变量加1;
       用户退出,统计变量减1;
       要求:用户刷新页面,统计变量不变;
      
    详细代码:default.aspx---用户登陆
             chat.aspx-----登陆成功后的界面
             global.asax----应用程序
      

    //default.aspx
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

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

            }


            
    protected void Button1_Click(object sender, EventArgs e)
            
    {
                Application.Lock();
                Application[
    "user"= (int)Application["user"+ 1;
                Application.UnLock();
                Response.Redirect(
    "chat.aspx?username=" + TextBox1.Text);
            }

        }

    }


    //chat.aspx
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

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

            }


            
    protected void Button1_Click(object sender, EventArgs e)
            
    {
                Application.Lock();
                Application[
    "user"= (int)Application["user"+ 1;
                Application.UnLock();
                Response.Redirect(
    "chat.aspx?username=" + TextBox1.Text);
            }

        }

    }

    //global.asax
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;

    namespace WebApplication1
    {
        
    public class Global : System.Web.HttpApplication
        
    {

            
    protected void Application_Start(object sender, EventArgs e)
            
    {
                Application[
    "user"= 0;
            }


            
    protected void Application_End(object sender, EventArgs e)
            
    {
                Application.Lock();
                Application[
    "user"= (int)Application["user"- 1;
                Application.UnLock();
            }
                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                    
        }

    }

     解果截图:---抱歉   不知道该怎么上传,传了几次都不可以.这里我们省略了验证部分
                                                       
    缺点:Vs2005的上述代码对浏览器的关闭没有反应,怎么弄?
    接下来我们说说Session
    它与浏览器同在,保存在内存中,有生命周期(timeout设定周期,一般是20min),所以它可以强制的终止,session.abandon()
    赋值:
    Session["var_name"]=var_name;
    Var_name=Session{"var_name"};
    常用属性
    SessionID
    IsNewSession
    Timeout
    常用方法
    Session.Abandon();//放弃这个Session
    Session.Removeall();//把所有的Session都清空掉
    Session.Remove("a");//把含有a的session都给remove掉
    Ada("")//有加的操作
    接下来我们用一个用户名和密码的传递来说明

    viewState的特点
    信息作为页面内往返时,ViewState工作
    信息作为隐藏域进行存储
    页面存在,ViewState就存在
    //它没有自动过期的问题,这是和Session的区别之一,如果录入成绩的时候,可以防止过期,这是优点

    Cookie的特点
    信息保存在客户端
    生命周期可以自行设定
    数据存储容量有限制(小于4096B)
    浏览器可能不支持
    像一些投票,我们只要把Cookie给删除,就可以投票投好多次呢,因为它用来验证谁的IP是放在本地的
    把表的内容放入到数据库里---(买书的购物车)

  • 相关阅读:
    iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
    iOS NSArray数组过滤
    App Store2016年最新审核规则
    iOS 根据字符串数目,自定义Label等控件的高度
    iOS 证书Bug The identity used to sign the executable is no longer valid 解决方案
    Entity FrameWork 增删查改的本质
    EF容器---代理类对象
    Entity FrameWork 延迟加载本质(二)
    Entity FrameWork 延迟加载的本质(一)
    Entity FrameWork 增删查改
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/993149.html
Copyright © 2011-2022 走看看