Reputation: 3519
I have a System.Web.UI.UserControl in my application which is to be used to display messages to the user, however after these messages are displayed to the user once I want them to clear (conditionally).
The simplified code I have now that I am trying to get to work is the following:
protected override void OnUnload(EventArgs e) {
if (_resetOnUnload) {
divMessageBlock.InnerHtml = "";
_resetOnUnload = false;
}
base.OnUnload(e);
}
However any changes to the view in the OnUnload event do not get transferred over on the next page load (form submit).
My question is how would I setup this user control to clear itself either before any messages can be added elsewhere or after the page has been rendered for the user and have stay that way?
Upvotes: 1
Views: 2651
Reputation: 124
Try this. Set the viewstate to save _resetOnUnload and then load it. OnLoad can stay the same, testing whether or not to clear.
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
if (_resetOnUnload) {
divMessageBlock.InnerHtml = "";
_resetOnUnload = false;
}
}
protected override void LoadViewState(object savedState) {
if (savedState != null) {
object[] myState = (object[])savedState;
_resetOnUnload = (bool)myState[0];
}
}
protected override object SaveViewState() {
object[] allStates = new object[]{ _resetOnUnload };
return allStates;
}
Upvotes: 1
Reputation: 9030
How about just outputting your message to a control with ViewState turned off? That way, your message will be displayed, and any subsequent Postback will clear the message.
Upvotes: 1
Reputation: 96561
I guess the OnPreRender
event is the last one, where you can change what the control's output should be. After that, the control has been rendered, and your changes will have no effect.
Upvotes: 0