使用AJAX和ViewState中的.NET不一致的行为 [英] Inconsistent behavior with AJAX and ViewState in .NET

查看:155
本文介绍了使用AJAX和ViewState中的.NET不一致的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现打刷新按钮,在我的浏览器会暂时搞砸了的ViewState的控件在UpdatePanel里面。

下面是我的情况:我做了一个自定义的WebControl存储在ViewState值。我把这个控制一个UpdatePanel内。当我打在我的浏览器的刷新按钮,它会暂时消灭在ViewState中的值。然而,在接下来的回发,这是在ViewState之前我打的价值观刷新魔术般地再次出现。

此行​​为螺钉了我的夏精。之后我打了刷新的控制返回到初始状态,因为ViewState的是空的的IsPostBack设置为false。然而,当我点击我的WebControl内的回发的控件之一,WebControl的将重新加载与那名在ViewState中之前,我打了相同的价值观刷新。

奇怪的是,当我使用AJAX这只是发生。当我的控制是一个UpdatePanel之外,火狐给了我它的标准的消息,要显示此页面时,Firefox必须发送进行早期(重发)的信息,将重复的任何操作(如搜索或订单确认)(取消) 。这是好的,因为至少该行为是一致的。但是,我绝对必须使用AJAX为此项目。

原来这就是我想要做的 - 我想使刷新行为一致。这将是最好的,如果打刷新并没有影响到的ViewState的。但是,如果它要消灭ViewState中,这很好,只要ViewState中保持一扫而光。没有这东西与价值消亡和重现。

哦,是的,这是我的例子code:

 使用系统;
使用System.Data这;
使用System.Configuration;
使用System.Linq的;
使用的System.Web;
使用System.Web.Security中;
使用System.Web.UI程序;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq的;

命名空间TestControls
{
    公共类系统testControl:WebControl的
    {
        INT _clickCount;
        布尔_mustUpdate;

        保护覆盖无效LoadViewState(对象savedState)
        {
            base.LoadViewState(savedState);

            _clickCount =((INT)的ViewState [的clickCount]);
            _mustUpdate =((布尔)的ViewState [mustUpdate]);
        }

        保护覆盖无效的OnLoad(EventArgs的发送)
        {
            base.OnLoad(E);

            Controls.Clear();
            ControlCreator();
        }

        私人无效ControlCreator()
        {
            标签tempLabel =新的Label();
            LiteralControl tempLiteral =新LiteralControl(< BR />< BR />中);
            LinkBut​​ton的tempLink =新的LinkBut​​ton();

            tempLink.ID =TESTLINK;
            tempLink.Text =点击我!;
            tempLink.Click + =新的EventHandler(tempLink_Click);

            tempLabel.ID =testLabel;
            tempLabel.Text = _clickCount.ToString();

            Controls.Add被(tempLabel);
            Controls.Add被(tempLiteral);
            Controls.Add被(tempLink);
        }

        无效tempLink_Click(对象发件人,EventArgs的)
        {
            _clickCount ++;
            _mustUpdate = TRUE;
        }

        保护覆盖无效在preRender(EventArgs的五)
        {
            base.On preRender(E);

            如果(_mustUpdate)
            {
                Controls.Clear();
                ControlCreator();

                _mustUpdate = FALSE;
            }
        }

        保护覆盖对象SaveViewState()
        {
            的ViewState [的clickCount] = _clickCount;
            的ViewState [mustUpdate] = _mustUpdate;

            返回base.SaveViewState();
        }
    }
}
 

解决方案

我刚刚重新阅读您的问题,并意识到我错过了什么......

您正在击球的浏览器的刷新按钮!

这是一个问题吗? 是!

当你点击浏览器上的刷新按钮,会发生什么? 它刷新当前页面。如果有一个回发,它会重新发送POST数据。

在AJAX回调可更新的ViewState至于网页关注,但打的浏览器刷新按钮将重新发布的旧的ViewState。

有大量的关于这个问题的讨论,你可以谷歌。

一个很简单的想法是可能存储在会话状态信息,而不是可能。这也同样可以有自己的问题,也当然。但它可能实现你的近期目标。

一个尖擦回发的历史(以prevent的重职)是重新回到同一页面了。

I'm finding that hitting the "Refresh" button on my browser will temporarily screw up the ViewState for controls inside an UpdatePanel.

Here's my situation : I made a custom WebControl that stores values in the ViewState. I put this control inside an UpdatePanel. When I hit the "refresh" button on my browser, it will temporarily wipe out the values in ViewState. However, on the next postback, the values that were in the ViewState before I hit "refresh" magically reappear.

This behavior screws up my webcontrol. After I hit "refresh," the control is returned to its initial state, since the ViewState is empty and IsPostBack is set to false. However, when I click on one of the postback controls within my WebControl, the WebControl will reload with the same values that were in ViewState before I hit "refresh."

Strangely, this only happens when I'm using AJAX. When my control is outside of an UpdatePanel, Firefox gives me it's standard message, "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier (Resend) (Cancel)." This is fine, because at least the behavior is consistent. However, I absolutely must use AJAX for this project.

So this is what I would like to do - I want to make the "refresh" behavior consistent. It would be best if hitting "refresh" didn't affect the ViewState at all. But if it has to wipe out the ViewState, that's fine, as long as the ViewState STAYS wiped out. None of this stuff with values disappearing and reappearing.

Oh yeah, and here's my example code :

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace TestControls
{
    public class TestControl : WebControl
    {
        int _clickCount;
        bool _mustUpdate;

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            _clickCount = ((int)ViewState["clickCount"]);
            _mustUpdate = ((bool)ViewState["mustUpdate"]);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Controls.Clear();
            ControlCreator();
        }

        private void ControlCreator()
        {
            Label tempLabel = new Label();
            LiteralControl tempLiteral = new LiteralControl("<br/><br/>");
            LinkButton tempLink = new LinkButton();

            tempLink.ID = "testLink";
            tempLink.Text = "Click me!";
            tempLink.Click += new EventHandler(tempLink_Click);

            tempLabel.ID = "testLabel";
            tempLabel.Text = _clickCount.ToString();

            Controls.Add(tempLabel);
            Controls.Add(tempLiteral);
            Controls.Add(tempLink);
        }

        void tempLink_Click(object sender, EventArgs e)
        {
            _clickCount++;
            _mustUpdate = true;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (_mustUpdate)
            {
                Controls.Clear();
                ControlCreator();

                _mustUpdate = false;
            }
        }

        protected override object SaveViewState()
        {
            ViewState["clickCount"] = _clickCount;
            ViewState["mustUpdate"] = _mustUpdate;

            return base.SaveViewState();
        }
    }
}

解决方案

I just re-read your question and realised I missed something...

You are hitting the browsers refresh button!

Is this a problem? YES!

What happens when you hit the refresh button on the browser? It refreshes the current page. If there was a postback, it will resend the post data.

The AJAX callback may update the ViewState as far as the page is concerned but hitting the browser refresh button will re-post the old ViewState.

There is a wealth is discussion on the matter which you can Google.

One very simple idea is to perhaps store the information in Session state instead perhaps. That too also can have its own issue too of course. But it may achieve your immediate goal.

A tip for wiping the history of a postback (to prevent the re-post) is to redirect back to the same page again.

这篇关于使用AJAX和ViewState中的.NET不一致的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆