If your class would be a control, it would have its own ViewState collection
(which would be saved and restored by the page framework).
However, if your class is stand-alone custom class, it either can take the
ViewState collection as a reference in and operate with it, or it can just
return the value(s) to the caller (Page) and let that handle operating with
the ViewState (there are examples of both in this thread).
Only controls (Page and its controls) have ViewState collection (each
control has its own) and therefore accessing it from a custom class needs
certain approach. Sessions you could access from class via
System.Web.HttpContext.Current.Session, but there's not similar way to
access ViewState.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Max" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> thanks,
> but it necessary to pass the viewstate reference from aspx page to class ?
> it's no possibile that class use viewstate without pass it from aspx page
> ?
> thanks
>
> "Teemu Keiski" <(E-Mail Removed)> ha scritto nel messaggio
> news:%(E-Mail Removed)...
>> Hi,
>>
>> As is described on following reply, yes you can, but it's not that good
>> approach. However, if you just want it done and don't care about what
>> else it might mean, then sure you can just pass the ViewState collection
>> from the Page to the class and use it (just pay attention to the
>> drawbacks I mentioned)
>>
>> --
>> Teemu Keiski
>> ASP.NET MVP, AspInsider
>> Finland, EU
>>
>> "Max" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> hi,
>>> from aspx page, i instance an class.
>>> in an method of this class, i need insert into viewstate a data.
>>> can i reference from class to viewstate of the page that have instance
>>> the class ?
>>> Thanks
>>>
>>>
>>> "Teemu Keiski" <(E-Mail Removed)> ha scritto nel messaggio
>>> news:(E-Mail Removed)...
>>>> You can do it so that Page calls a method of the class, which returns
>>>> something which is again placed into ViewState or Session by the Page.
>>>> This way class doesn't need to know about the ViewState collection like
>>>> the key used and so on, which is actually better for many reasons
>>>>
>>>> -no chance for name collisions
>>>> -class library is usable from other type projects than web projects
>>>> (Windows Forms, or WinServices...)
>>>> -class does only it's job, no extra reponsibilities
>>>> -clear workings for the class consumer
>>>>
>>>> 'Code on the Page
>>>> Dim cIns As New TheClassInstance()
>>>> ViewState("data") = cIns.returnSomething()
>>>>
>>>> 'Then again getting it back
>>>> ...
>>>>
>>>> --
>>>> Teemu Keiski
>>>> ASP.NET MVP, AspInsider
>>>> Finland, EU
>>>>
>>>>
>>>>
>>>> "Max" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>>I need an vb.net class that is invoked from aspx page, that use the
>>>>>viewstate/session object.
>>>>> This class must be store the information into viewstate/session.
>>>>>
>>>>> Can you give me an example ?
>>>>> Thanks
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
|