Forum Moderators: open

Message Too Old, No Replies

User controls don't maintain state

         

garann

12:18 am on Jun 13, 2006 (gmt 0)

10+ Year Member



On the adivce of this forum, I'm building a user control to show news items. One of the options users have is collapsing the items, so they only see the headlines and can click the ones they're interested in.

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!

TheNige

1:08 am on Jun 13, 2006 (gmt 0)

10+ Year Member



You will probably need to add the controls to the page on Page_Init as the view state gets loaded after than and before Page_Load.

[aspnet.4guysfromrolla.com...]

garann

6:01 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



Thanks for the hint, but I'm not quite following... I'm adding the control in the markup of the page, not programmatically. Are you talking about the control's Init event? My concern is that it doesn't appear the changes to my variable are being reflected in the viewstate - Page_Load resets the value, which it should only do if my variable's been overwritten or not changed to begin with.

TheNige

8:52 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



Sorry, I thought you were adding the user controls programatically to your page as discussed in the other thread.

where are you initializing the "headlinesOnly" variable? Show your Page_Load code.

garann

9:13 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



At this point I'm just testing the control to implement as discussed previously. So it's just in the markup of a test page. :)

I'm not initializing it - just letting it initialize itself to false (or I assume that's what it does). It's read in Page_Load, but not set.

garann

5:58 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



Additional note: If I set the variable's value in the page that contains the control, it displays as it should. But it still never changes.

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?

garann

7:33 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



Nevermind. I gave it up and decided to put the variable into the physical form, since that seemed to be the only way to keep it in the request. Too bad no one knows how to make the much touted user control maintain its state - chalk another point up for JSF.

TheNige

9:08 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



If all that you were doing is setting a variable in the code then it won't maintain that value.

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.

garann

10:42 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



Ah. That's what I was looking for - thanks. For some reason, I thought I'd been able to maintain the state of variables within a codebehind on other pages. Bizarre. I'm sure I'll have occasion to use the ViewState.Add() thing in future controls, though. I appreciate it!