代码之家  ›  专栏  ›  技术社区  ›  Mausimo

我可以在viewstate中存储队列吗?只存储我添加到队列的第一个项目

  •  0
  • Mausimo  · 技术社区  · 15 年前

    编辑:刷新浏览器时,viewstate不会更改,因此即使写入viewstate,也不会保存数据。呸!

    嘿,正如问题所述,我正试图在视图状态中存储队列(跟踪回发和刷新以阻止表单重新提交)。

    不管我添加了多少项,队列都只保存了1个计数(添加到队列中的第一个项)。

    以下只是视图状态代码:

        // New Queue of strings
    private Queue<string> sTemp;
    
     private Queue<string> p_tempQue
    {
        set
        {
            ViewState["sTemp"] = value;
        }
        get
        {
            return (Queue<string>)ViewState["sTemp"];
        }
    }
    
    //BasePage constructor 
    public BasePage()
     {
            //create a Queue of string
            //sTemp = new Queue<string>();
            this.Load += new EventHandler(this.Page_Load);
            this.Init += new EventHandler(this.Page_Init);
    
     }
    
    
    //In the 'page_Init' event we have created a simple hidden field by name 'hdnGuid' which is attached to the page on the first hit itself. 
    protected void Page_Init(object sender, EventArgs e)
    {
        //initializing the hidden field
    
        //create a hidden field with a ID
        HiddenField hdnGuid = new HiddenField();
        hdnGuid.ID = "hdnGuid";
    
        //if it is the first time the page is loaded, create a new guid and assign it as the hidden field value 
        if (!Page.IsPostBack)
            hdnGuid.Value = Guid.NewGuid().ToString();
    
        //add the hidden field to the page
        Page.Form.Controls.Add(hdnGuid);
    }
    
    //In the 'page_Load' event we check if the hidden field value is same as the old value. In case the value is not same that means it's a 'postback'
    //and if the value is same then its 'refresh'. As per situation we set the 'httpContent.Items["Refresh"]' value. 
    protected void Page_Load(object sender, EventArgs e)
    {
        if(p_tempQue != null)
        sTemp = p_tempQue;
        else
            sTemp = new Queue<string>();
    
        //The hdnGuid will be set the first time page is loaded, else the hdnGuid 
        //will be set after each time the form is submitted using javascript.
    
        //assign the hidden field currently on the page for manipulation
        HiddenField h1 = (HiddenField)(Page.Form.FindControl("hdnGuid"));
        //create an instance of the GuidClass
        GuidClass currentGuid = new GuidClass();
        //set the GuidClass Guid property to the value of the hidden field
        currentGuid.Guid = h1.Value;
    
    
        //check to see if the Queue of strings contains the string which is the current Guid property of the GuidClass
        //if the are equal, then the page was refreshed
        if (sTemp.Contains<string>(currentGuid.Guid))
        {
            //adds item as key/value pair to share data between an System.Web.IHttpModule interface and an System.Web.IHttpHandler interface during an HTTP request.
            System.Web.HttpContext.Current.Items.Add("IsRefresh", true);
        }
        //if they are not requal, the page is not refreshed
        else
        {
           //if the current Guid property in the GuidClass is not null or not an empty string
           //add the new Guid to the Queue
           if (!(currentGuid.Guid.Equals(null) || currentGuid.Guid.Equals("")))
              sTemp.Enqueue(currentGuid.Guid);
    
         System.Web.HttpContext.Current.Items.Add("IsRefresh", false);
        }
    
        p_tempQue = sTemp;
    }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Chris Marisic    15 年前

    虽然这不是直接解决您的问题,但使用“刷新”按钮消除重复表单提交的标准方法是 Post/Redirect/Get . 但是,您可能仍然需要防止双击提交按钮。

        2
  •  0
  •   Tejs    15 年前

    它将把整个数据结构存储在viewstate中——这是好的也是坏的。如果您坚持一个非常大的对象,它将极大地膨胀您的视图状态。

        3
  •  0
  •   Mausimo    14 年前

    问题是我正在测试刷新,刷新时视图状态变量没有更新。因此,stemp只会增加回发次数(单击提交按钮)。这种方法对我不起作用。我已将代码更改为使用httpmodule。