C # convert viewstate to bool array

How can I convince viewstate in a bool array

private bool[] clientCollapse
{
    get { return Convert.ToBoolean(ViewState["Collapse"]); }
    set { ViewState["Collapse"] = value; }
}

Any ideas ???

+3
source share
6 answers
private bool[] clientCollapse
{
    get { return (bool[])ViewState["Collapse"]; }
    set { ViewState["Collapse"] = value; }
}

if it works, if you set these values ​​only using this property, otherwise you can, but there the other type and cast will not work

BTW general naming conventions for C # require property names to start with capital: ClientCollapse

+3
source

try changing your getter to:

get { return ViewState["Collapse"] as bool[]; }

this will return null if it is not set.

+1
source

:

private bool [] clientCollapse
{
    get { return (bool[]) ViewState["Collapse"]); }
    set { ViewState["Collapse"] = value; }
}

ASP.NET .

+1

:

private bool[] clientCollapse
{
    get { return (bool[])ViewState["Collapse"] ?? new bool[0]; }
    set { ViewState["Collapse"] = value; }
}
+1
private bool [] clientCollapse
{
    get { return (ViewState["Collapse"] as bool[]); }
    set { ViewState["Collapse"]; }
}
+1

, ViewState<byte[]>.GetTypedData(key):

public static class ViewStateExtensions
    {

         public static T GetTypedData<T>(this StateBag bag, string key)
         {
             return (T) bag[key];
         }

         public static void SetTypedData<T>(this StateBag bag, string key, T value)
         {
             bag[key] = value;
         }

    }
+1

Source: https://habr.com/ru/post/1780444/


All Articles