Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maintaining State of Properties within multiple WUCs 1

Status
Not open for further replies.

steverbs

Programmer
Jul 17, 2003
253
GB
Hi all.

Having a little trouble with a single-page-architecture web-application. I am building the application out of WUCs that contain WUCs etc and have tried to make them as re-usable as possible, so that I have controls such as AddressDetails, UserDetails, etc. Now I am running into trouble maintaining the state of these controls.

Take the AddressDetails WUC for example. This has textboxes for each of the address details (street, city, etc) and also has a property named myAddress, which holds the address that the control is being used to edit so that its (the address) values can be used to load the form fields and compare newly entered values against the old ones etc. The only way that I know of to maintain state for properties is to use the Session collection: 'Me.Session("currentAddressForEditing") = Me.myAddress'. The problem is that this gets tricky when several WUCs are all editing the same address, and impossible when there are more than one of the same AddressDetails WUC on the page, so that they are all grabbing at the 'currentAddressForEditing' in the session collection. The latter of which is causing the greatest problems.

Any help that you have to offer here would be invaluable.

Thank you.

Stephen.
 
Why does regular ViewState not work for you in this situation? I think I'm missing something.

-paul

ps. You'll find that the more you use ASP.NET, the less you'll use UC's. They're not bad, and I do use them, it's just that I used to OVERuse them, and it gets very messy when you do that (as I think you're seeing here).

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Well, I had taken a look at ViewState, but have been unsure as to exactly what it does. I have read alot of articles about it, all of which say very little except that it is used to store control state information.

But, now that you bring this up I think I see where you are coming from and I might be starting to get how I could use it. Am I correct in guessing that even though I use the same WUC twice in a single form, the viewstate collection for each of these controls will be separate, thus preventing the controls from grabbing at the same collection item?

In other words. Will my problem be solved by using:

Me.ViewState("currentAddressForEditing") = Me.myAddress

As opposed to:

Me.Session("currentAddressForEditing") = Me.myAddress

Steve.
 
Yes, that is correct. Through the use of the INamingContainer interface, ASP.NET automatically keeps UC values separated. But it gets even better.

By default, ALL form objects will automatically have their state persisted in ViewState, so storing their values is not necessary. You get that for free. When the page posts back, the values will still be in the textboxes, the same item will still be chosen in the dropdown, etc...

ViewState is one of the best things that ever happened to web development. The only thing to keep in mind, though, is that the value is persisted in the __VIEWSTATE form variable, and is posted back and forth between the server and the client.

Don't keep sensitive information in there, and don't let it get too overbloated. Like I said, by default, all ViewState is turned on, so literally everything on your page is stored in there. If postbacks start seeming really sluggish, then a likely culprit is an overbloated ViewState. Turn it off for controls where you don't need it.

Also, you can manually add things to the ViewState (AKA the StateBag) by directly accessing it:

ViewState[key] = value;

:)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Yeah, I'd known about viewstate maintaining the state of the controls automatically, but I had no idea that I could also programatically add data to it in the codebehind. This ASP.NET just keeps getting better and better.

I've also nominated you for tipmaster of the week as you keep getting me out of trouble! Thank you for all of your help.

Regards.

Stephen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top