Problem with control and ViewState

  • Thread starter Thread starter Fernando A. Gómez F.
  • Start date Start date
F

Fernando A. Gómez F.

Hello all.

I'm creating a custom control (i.e. inherited from WebControl) which
happens to create a Label and a TextBox. Nothing too fancy. The control
is showing well. However, whenever a PostBack is fired, the entered data
is lost and reset into it's first state.

AFAIK this is normal, since the controls are created every time a
PostBack is fired. Therefore I should manage the ViewState.

Am I so far right?

The thing is, I'm having a hard time understanding how to manage the
ViewState. As I understand it, I should store any value I'd like to
persist in the ViewState dictionary. I have something like:

public string Value
{
get
{
if (ViewState["value"] == null)
ViewState["value"] = string.Empty;

return ViewState["value"] as string;
}
set { ViewState["value"] = value; }
}

However, I'm not able to change anything. When I change (in the IE) the
text on my displayed TextBox and fire the PostBack (i.e. by clicking on
a Button), I don't get whatever the user typed because now it gets the
value from the ViewState. Yet if I try to get the value from
(say)_myTextBox.Text I won't, since the PostBack already reseted the
values.

I tried another approach. I subscribed a delegate to the
TextBox.TextChanged event so that I could save the text input as soon as
the user finished typing it. Yet it yields the same behavior, since the
PostBack resets all the content before I get to the event handling.

After doing some further googling, I found that I could override the
LoadViewState and SaveViewState methods. Yet I'm not able to do so
because I can't get _myTextBox.Text's value because it always gets erased.

I know for sure that I'm missing something, but I can't figure out what
that something could be. Any help will be really appreciated. Thanks in
advance.

Regards,
Fernando.
 
At what stage during postback do you create the control? You should do it in
Page PreInit or Init event, then it will pick up the viewstate.
 
Eliyahu said:
At what stage during postback do you create the control? You should do
it in Page PreInit or Init event, then it will pick up the viewstate.

Hello Eliyahu,

thanks for your answer. The control is added to my page via an ASP.NET
tag. Something like:

// default.aspx

<html ...>
<body>
<form ...>
<boc:TextElement ID="elem1" runat="server" Value="Hello World" />
...
</form>
</body>
</html>

So I guess the control is created in standard way.

The children controls (i.e. my TextBox) is created in the custom
control's overrided CreateChildControls method.

Thanks for your help.
 
Fernando said:
Hello all.

I'm creating a custom control (i.e. inherited from WebControl) which
happens to create a Label and a TextBox. Nothing too fancy. The control
is showing well. However, whenever a PostBack is fired, the entered data
is lost and reset into it's first state.

AFAIK this is normal, since the controls are created every time a
PostBack is fired. Therefore I should manage the ViewState.

Am I so far right?

The thing is, I'm having a hard time understanding how to manage the
ViewState. As I understand it, I should store any value I'd like to
persist in the ViewState dictionary. I have something like:

public string Value
{
get
{
if (ViewState["value"] == null)
ViewState["value"] = string.Empty;

return ViewState["value"] as string;
}
set { ViewState["value"] = value; }
}

However, I'm not able to change anything. When I change (in the IE) the
text on my displayed TextBox and fire the PostBack (i.e. by clicking on
a Button), I don't get whatever the user typed because now it gets the
value from the ViewState. Yet if I try to get the value from
(say)_myTextBox.Text I won't, since the PostBack already reseted the
values.

I tried another approach. I subscribed a delegate to the
TextBox.TextChanged event so that I could save the text input as soon as
the user finished typing it. Yet it yields the same behavior, since the
PostBack resets all the content before I get to the event handling.

After doing some further googling, I found that I could override the
LoadViewState and SaveViewState methods. Yet I'm not able to do so
because I can't get _myTextBox.Text's value because it always gets erased.

I know for sure that I'm missing something, but I can't figure out what
that something could be. Any help will be really appreciated. Thanks in
advance.

Regards,
Fernando.

I found some documentation saying that in these cases I should inherit
from CompositeControl rathern than WebControl. Now this is much better,
everything is working fine.
 
Back
Top