locked
ViewState in a static method? RRS feed

  • Question

  • User888441741 posted
    can somebody tell me, how can I access a ViewState variable inside of a static method?  

    i'm using in custom control

    My code here:
    public static List<cast> GetCastByLetter(string crewType, string letterFilter, string sortExpression)
    {  ...

    switch (ViewState["_LetterFilter"]) { ... }
    }



    and my error :-)

    An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Control.ViewState.get'

    Friday, March 28, 2008 11:08 AM

Answers

  • User1186257875 posted

    Why does it have to be static?  If you make it static you cant access viewstate!  Static methods are class level methods and do not have access to instance variables!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 28, 2008 2:57 PM

All replies

  • User1186257875 posted

    ViewState is on the page object therefore you cant access it!  You could pass it into the static method though?

    Friday, March 28, 2008 11:16 AM
  • User1186257875 posted

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StaticMethod(this.ViewState);
        }
        public static void StaticMethod(IDictionary viewState)
        {
           
        }
    }

    Friday, March 28, 2008 11:18 AM
  • User888441741 posted

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StaticMethod(this.ViewState);
        }
        public static void StaticMethod(IDictionary viewState)
        {
           
        }
    }

    i'm not sure if this is a the way to go

    i'm building the custom control and i have:

    one public static method
    few public properties exposed (like, pageSize, usePage......)

    the user can set those properties on the page directive as a properties.....

    and on my custom control, i would like to read what are the properties user have selected....i think its a good idea to tell user to pass viewstate

    any thoughts?

    thanks.

     

    Friday, March 28, 2008 12:33 PM
  • User1186257875 posted

    Why does it have to be static?  If you make it static you cant access viewstate!  Static methods are class level methods and do not have access to instance variables!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 28, 2008 2:57 PM