Forum Moderators: open
I'm having a problem toggling the state of my "headlines only" variable. I can see the event handler run, but when I change the variable the changes don't stick and the page load handler runs again with the unchanged value.
Here's part of my code. Let me know if I should post more:
public Boolean headlinesOnly;
...
private void InitializeComponent()
{
this.headlineToggle.ServerClick += new System.EventHandler(this.headlineToggle_ServerClick);
this.filters.ServerChange += new System.EventHandler(this.filters_ServerChange);
this.Load += new System.EventHandler(this.Page_Load);
}
...
protected void headlineToggle_ServerClick(object sender, System.EventArgs e) {
if (headlinesOnly == true) {
this.headlinesOnly = false;
} else {
this.headlinesOnly = true;
}
}
Thanks for any help!
In my event handler, I thought I might not be referring to the right instance, so I set the variable like this:
((newsReader)((HtmlAnchor)sender).Parent).headlinesOnly = true; That specific attempt didn't work, but does that seem like the right track?
Also, it appears that the event handler runs after Page_Load runs twice. In JSF, there's an "immediate" setting for actions that tells the framework to run the action code before trying to redraw the page or do anything else. Does ASP.NET have anything similar?
ViewState maintains the state for controls on the page...controls being things like the buttons, text boxes, etc.
The same goes for User Controls. The state of the server controls in the user control are maintained.
Any private or public members you create will not be persisted. You'll have to store those values someplace like the Session or the page's ViewState.
example:
Dim age as integer
age = CInt(txtAge.text)
ViewState.Add("Age", age)
When a postback happens and you want to get the value of Age then you need to get it from viewstate.
Dim age as integer
age = CInt(ViewState.Item("Age"))
Once the page executes on the server it is gone. The server doesn't keep it around to remember what values you set. The control state is sent back to the client in the ViewState control (use view source to see the base64 string). When the client posts this back to the server it will then assign the viewstate values back to the controls on the newly executing page.
Without seeing all of you code it is hard to see what you are trying to accomplish.