简体   繁体   中英

Custom Control (ascx) LoadViewState error: Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair'

I am trying to migrate the code snippet below from an ASP.NET Website to an Web Application Project.

I'm getting the following error: Unable to cast object of type 'System.Object[]' to type 'System.Web.UI.Pair' .

The error is generated on this line (in LoadViewState method): base.LoadViewState(VS_all[0]);

I don't understand why, because the method accepts Object, so why does it care?

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]);

        }
    }

}

If you use Reflector or JustDecompile, you'll see internally LoadViewState requires Pair even though it accepts an object type. So if you refactor your Save to return:

return new Pair(VS_base, VS_all);

and change your LoadViewState to:

var pa

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

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

It should resolve the issue. Taken from the original source (using JustDecompile):

LoadViewState:

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

Thanks to Brian Mains for his solution. Below is the code with his suggestions incorporated into it in case someone needs similar help.

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]);

        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM