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.
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.