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

时间:2010-04-29 18:02:27

标签: c# asp.net

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

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

无论我添加了多少项目,队列只会保存一个计数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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

它将整个数据结构存储在ViewState中 - 这既好又坏。如果你持久化一个非常大的对象,它将极大地膨胀你的ViewState。

答案 2 :(得分:0)

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