自定义控件(ascx)LoadViewState错误:无法将类型为'System.Object []'的对象强制转换为'System.Web.UI.Pair'

时间:2016-03-12 02:02:16

标签: c# asp.net .net viewstate

我正在尝试将以下代码段从ASP.NET网站迁移到Web应用程序项目。

我收到以下错误:Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair'

在此行上生成错误(在LoadViewState方法中):base.LoadViewState(VS_all[0]);

我不明白为什么,因为该方法接受Object,为什么它关心?

public partial class Skins_DownloadGridView : System.Web.UI.UserControl
{

    private const string CtlIdent = "Controls_DownloadGridView";
    private string _Cat = "NONE";

    private string _SubCat = "";
    private string _SubCatHeader;
    private string _FileNameHeader;
    private string _FileDescHeader;
    private string _ActionsHeader;
    private bool _debug = false;
    private bool _enabled = false;

    private EOL.UI.Web.GVUtility GVUtil = new EOL.UI.Web.GVUtility();
    protected override object SaveViewState()
    {
        EOL.Common.Logging.LogDebug(string.Format("{0}.SaveViewState", CtlIdent), EOL.Common.Constants.DebugCodes.UserControls);
        object VS_base = base.SaveViewState();
        object[] VS_all = new object[9];

        VS_all[0] = VS_base;
        VS_all[1] = _Cat;
        VS_all[2] = _SubCat;
        VS_all[3] = _SubCatHeader;
        VS_all[4] = _FileNameHeader;
        VS_all[5] = _FileDescHeader;
        VS_all[6] = _ActionsHeader;
        VS_all[7] = _debug;
        VS_all[8] = _enabled;
        return VS_all;

    }

    protected override void LoadViewState(object VS_saved)
    {
        EOL.Common.Logging.LogDebug(string.Format("{0}.LoadViewState", CtlIdent), EOL.Common.Constants.DebugCodes.UserControls);
        if ((VS_saved != null))
        {
            object[] VS_all = new object[]{VS_saved};

            if ((VS_all[0] != null))
                ***base.LoadViewState(VS_all[0]);*** <--- Error generated here.
            if ((VS_all[1] != null))
                _Cat = Convert.ToString(VS_all[1]);
            if ((VS_all[2] != null))
                _SubCat = Convert.ToString(VS_all[2]);
            if ((VS_all[3] != null))
                _SubCatHeader = Convert.ToString(VS_all[3]);
            if ((VS_all[4] != null))
                _FileNameHeader = Convert.ToString(VS_all[4]);
            if ((VS_all[5] != null))
                _FileDescHeader = Convert.ToString(VS_all[5]);
            if ((VS_all[6] != null))
                _ActionsHeader = Convert.ToString(VS_all[6]);
            if ((VS_all[7] != null))
                _debug = Convert.ToBoolean(VS_all[7]);
            if ((VS_all[8] != null))
                Enabled = Convert.ToBoolean(VS_all[8]);

        }
    }

}

2 个答案:

答案 0 :(得分:0)

如果你使用Reflector或JustDecompile,你会看到内部LoadViewState需要Pair,即使它接受一个对象类型。因此,如果您重构Save以返回:

return new Pair(VS_base, VS_all);

并将LoadViewState更改为:

var pa

ir = (Pair)savedState;
base.LoadViewState(pair.First);

var VS_all = (object[])pair.Second;
//Load

它应该解决这个问题。取自原始来源(使用JustDecompile):

LoadViewState:

if (savedState != null) {
     Pair pair = (Pair)savedState;

答案 1 :(得分:0)

感谢Brian Mains的解决方案。下面是代码,如果有人需要类似的帮助,他的建议会被纳入其中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
public partial class Skins_DownloadGridView : System.Web.UI.UserControl
{

    private const string CtlIdent = "Controls_DownloadGridView";
    private string _Cat = "NONE";

    private string _SubCat = "";
    private string _SubCatHeader;
    private string _FileNameHeader;
    private string _FileDescHeader;
    private string _ActionsHeader;
    private bool _debug = false;
    private bool _enabled = false;

    private UCLA.EOL.UI.Web.GVUtility GVUtil = new UCLA.EOL.UI.Web.GVUtility();
    protected override object SaveViewState()
    {
        UCLA.EOL.Common.Logging.LogDebug(string.Format("{0}.SaveViewState", CtlIdent), UCLA.EOL.Common.Constants.DebugCodes.UserControls);
        object VS_base = base.SaveViewState();
        object[] VS_all = new object[9];

        VS_all[0] = VS_base;
        VS_all[1] = _Cat;
        VS_all[2] = _SubCat;
        VS_all[3] = _SubCatHeader;
        VS_all[4] = _FileNameHeader;
        VS_all[5] = _FileDescHeader;
        VS_all[6] = _ActionsHeader;
        VS_all[7] = _debug;
        VS_all[8] = _enabled;

        //return VS_all;
        return new Pair(VS_base, VS_all); // <-- Change

    }

    protected override void LoadViewState(object VS_saved)
    {
        UCLA.EOL.Common.Logging.LogDebug(string.Format("{0}.LoadViewState", CtlIdent), UCLA.EOL.Common.Constants.DebugCodes.UserControls);
        if ((VS_saved != null))
        {
            object[] VS_all = new object[]{VS_saved};


            // *** CHANGE START ***

            Pair pair = (Pair)VS_saved;

            if ((VS_all[0] != null))
                base.LoadViewState(pair.First);

            VS_all = (Object[])pair.Second;

            // *** CHANGE  END  ***                

            if ((VS_all[1] != null))
                _Cat = Convert.ToString(VS_all[1]);
            if ((VS_all[2] != null))
                _SubCat = Convert.ToString(VS_all[2]);
            if ((VS_all[3] != null))
                _SubCatHeader = Convert.ToString(VS_all[3]);
            if ((VS_all[4] != null))
                _FileNameHeader = Convert.ToString(VS_all[4]);
            if ((VS_all[5] != null))
                _FileDescHeader = Convert.ToString(VS_all[5]);
            if ((VS_all[6] != null))
                _ActionsHeader = Convert.ToString(VS_all[6]);
            if ((VS_all[7] != null))
                _debug = Convert.ToBoolean(VS_all[7]);
            if ((VS_all[8] != null))
                Enabled = Convert.ToBoolean(VS_all[8]);

        }
    }

}