devaid
asked on
Viewstate with Custom Controls
Hi people,
I've created a custom control with 3 textboxes amongst otherstuff.
Onpostback my controls lose their values. I've played with the StateBag / Viewstate but cannot get it to work.
Any places I could read up on or any sugested ideas.
Code Snippets
private bool _isTrackingViewState = true;
private StateBag _viewState;
#region IStateManager Members
protected override StateBag ViewState
{
get
{
if(_viewState == null)
{
_viewState = new StateBag(false);
if(_isTrackingViewState)
{
((IStateManager)_viewState ).TrackVie wState();
}
}
return _viewState;
}
}
void IStateManager.TrackViewSta te()
{
// TODO: Add FAQ.TrackViewState implementation
if(_viewState != null)
{
((IStateManager)_viewState ).TrackVie wState();
}
_isTrackingViewState = true;
}
bool IStateManager.IsTrackingVi ewState
{
get
{
return _isTrackingViewState;
}
}
object IStateManager.SaveViewStat e()
{
// TODO: Add FAQ.SaveViewState implementation
if (_viewState != null)
{
return ((IStateManager)_viewState ).SaveView State();
}
return null;
}
void IStateManager.LoadViewStat e(object savedState)
{
// TODO: Add FAQ.LoadViewState implementation
if(savedState != null)
{
((IStateManager)ViewState) .LoadViewS tate(saved State);
}
}
#endregion
I've created a custom control with 3 textboxes amongst otherstuff.
Onpostback my controls lose their values. I've played with the StateBag / Viewstate but cannot get it to work.
Any places I could read up on or any sugested ideas.
Code Snippets
private bool _isTrackingViewState = true;
private StateBag _viewState;
#region IStateManager Members
protected override StateBag ViewState
{
get
{
if(_viewState == null)
{
_viewState = new StateBag(false);
if(_isTrackingViewState)
{
((IStateManager)_viewState
}
}
return _viewState;
}
}
void IStateManager.TrackViewSta
{
// TODO: Add FAQ.TrackViewState implementation
if(_viewState != null)
{
((IStateManager)_viewState
}
_isTrackingViewState = true;
}
bool IStateManager.IsTrackingVi
{
get
{
return _isTrackingViewState;
}
}
object IStateManager.SaveViewStat
{
// TODO: Add FAQ.SaveViewState implementation
if (_viewState != null)
{
return ((IStateManager)_viewState
}
return null;
}
void IStateManager.LoadViewStat
{
// TODO: Add FAQ.LoadViewState implementation
if(savedState != null)
{
((IStateManager)ViewState)
}
}
#endregion
ASKER
The override was a stab in the dark as I couldn't get it to work.
The code just generates 3 textboxes with some table layouts and text.
The code just generates 3 textboxes with some table layouts and text.
ASKER
Ah ha,
figured out why.
I'm creating the objects at runtime like this:
CustControl = myCustC = new CustControl (3);
So at runtime it creates an instance on each postback thus not ViewState Data.
Any ways around this? --- Increased points from 250 to 500
figured out why.
I'm creating the objects at runtime like this:
CustControl = myCustC = new CustControl (3);
So at runtime it creates an instance on each postback thus not ViewState Data.
Any ways around this? --- Increased points from 250 to 500
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post your code? I think there might be something wrong.