在数据库中存储Viewstate禁用了我的asp:Image Button事件

时间:2011-09-28 11:57:08

标签: c# asp.net viewstate

我按照指南here to save my ViewState into the database进行了操作。我做了一些更改以适应我的项目代码设计,但必不可少的部分在那里。但是当我植入这个解决方案时,所有我的asp:ImageButtons事件都停止工作,但是常规的asp:Buttons似乎有效。为什么asp:ImageButtons的事件不起作用?

代码:

public class DatabasePageStatePersister : PageStatePersister
{        
    //This object handles the saving and loading from database
    CiroLightLibrary.BLL.ViewState myViewState;

    public DatabasePageStatePersister(Page p, string GUID): base(p)
    {
        myViewState = new CiroLightLibrary.BLL.ViewState();
        myViewState.GUID = GUID;
    }

    public override void Load()
    {
        myViewState.Load();
        this.ViewState = this.StateFormatter.Deserialize(myViewState.Value);
    }

    public override void Save()
    {
        myViewState.Value = this.StateFormatter.Serialize(this.ViewState);
        myViewState.Save();
    }
}

public class PageViewStateDatabaseStored : Page
{
    private PageStatePersister _PageStatePersister;

    protected override System.Web.UI.PageStatePersister PageStatePersister
    {
        get
        {
            if (_PageStatePersister == null)
            {
                CiroLightLibrary.BLL.ViewState myViewState = new ViewState();

                if (Request["__DATABASE_VIEWSTATE"] != null)
                    myViewState.GUID = Request["__DATABASE_VIEWSTATE"].ToString();
                else
                    myViewState.GUID = Guid.NewGuid().ToString();

                _PageStatePersister = new DatabasePageStatePersister(this, myViewState.GUID);

                Literal l = new Literal();
                l.Text = "<div><input type=\"hidden\" name=\"__DATABASE_VIEWSTATE\" value=\"" + myViewState.GUID + "\" /></div>";
                this.Form.Controls.Add(l);
            }

            return _PageStatePersister;
        }
    }
}

还有一个测试页面:

public partial class test : PageViewStateDatabaseStored
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            imgButton1.CommandArgument = "1";
            btnButton1.CommandArgument = "1";
        }
    }

    protected void imgButton_OnCommand(object sender, CommandEventArgs e)
    {
        Response.Write(e.CommandArgument.ToString());
    }

    protected void imgButton_OnClick(object sender, EventArgs e)
    {
        Response.Write("Click");
    } 
}

Asp.net Page

    <form runat="server">
        <asp:ImageButton ID="imgButton1" runat="server" OnCommand="imgButton_OnCommand" />
        <asp:ImageButton ID="imgButton2" runat="server" OnClick="imgButton_OnClick" />

        <asp:Button ID="btnButton1" runat="server" OnCommand="imgButton_OnCommand" />
        <asp:Button ID="btnButton2" runat="server" OnClick="imgButton_OnClick" /> 
    </form>

2 个答案:

答案 0 :(得分:0)

我尝试覆盖LoadPageStateFromPersistenceMedium()SavePageStateToPersistenceMedium(),现在我的asp:ImageButtons也会触发事件。

public class PageViewStateDatabaseStored : Page
{
    protected override object LoadPageStateFromPersistenceMedium()
    {
        CiroLightLibrary.BLL.ViewState myViewState = new ViewState();

        if (Request["__DATABASE_VIEWSTATE"] != null)
        {
            myViewState.GUID = Request["__DATABASE_VIEWSTATE"].ToString();
            myViewState.Load();
        }

        LosFormatter myFormatter = new LosFormatter();

        return myFormatter.Deserialize(myViewState.Value);
    }

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        CiroLightLibrary.BLL.ViewState myViewState = new ViewState();

        if (Request["__DATABASE_VIEWSTATE"] != null)
            myViewState.GUID = Request["__DATABASE_VIEWSTATE"].ToString();
        else
            myViewState.GUID = Guid.NewGuid().ToString();

        LosFormatter myFormatter = new LosFormatter();
        StringWriter myStringWriter = new StringWriter();
        myFormatter.Serialize(myStringWriter, viewState);

        myViewState.Value = myStringWriter.ToString();
        myViewState.Save();

        ScriptManager.RegisterHiddenField(this, "__DATABASE_VIEWSTATE", myViewState.GUID);
    }
}

答案 1 :(得分:0)

我知道这是一篇旧帖....但答案很简单,就是你要保存视图状态而不是控制状态。

 public override void Load()
{
    myViewState.Load();
    var pair = (Pair)this.StateFormatter.Deserialize(myViewState.Value);
    this.ViewState = pair.First;
    this.ControlState = pair.Second;

}

public override void Save()
{
    myViewState.Value = this.StateFormatter.Serialize(new Pair(this.ViewState, this.ControlState));
    myViewState.Save();
}

我个人从HiddenFieldPageStatePersister继承我的PageStatePersister,然后在我的save方法中将guid写入viewstate属性并将controlstate设置为null,然后调用基本Save方法。在Load中,调用load the base.load然后从viewstate属性获取GUID,然后将db获取的值推送到viewstate,controlstate属性。这样我们就不会修改控制树....就像这样:

public class MyPageStatePersister : System.Web.UI.HiddenFieldPageStatePersister
{


     public MyPageStatePersister(Page page)
        : base(page)
    {
     }

    public override void Load()
    {
        base.Load();
        this.CurrentKey = (Guid)this.ViewState;
        var s = this.CurrentKey;
        var state = SomeDAOManager.GetState(s);
        if (state != null)
        {
            this.ViewState = state.First;
            this.ControlState = state.Second;
        }
        else
        {
            this.ControlState = null;
            this.ViewState = null;
        }

    }

    public Guid CurrentKey {get;set;}


    public override void Save()
    {
        if (CurrentKey == Guid.Empty)
        {
            this.CurrentKey = Guid.NewGuid();
        }
        Guid guid  = CurrentKey;
        SomeDAOManager.SaveState(guid, new Pair(this.ViewState, this.ControlState));
        this.ViewState = guid;
        this.ControlState = null;
        base.Save();

    }
}