Gain Control Of Your Control State (haacked.com)

published 2 years, 10 months ago, submitted by HaackedHaacked(5090) 2 years, 10 months ago

A Primer on the less well known cousin to ViewState, the Control State. Contains an interesting demo of the two types of state persistence side by side.

2 comments | category: | Views: 4

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:

Comments:

posted by yesthatmcgurkyesthatmcgurk(3805) 2 years, 10 months ago

Good article. I, however, prefer to create a "bag"--a simple struct--to hold state when I'm using Control State with an inherited control. It makes my code a bit easier to understand and maintain than sticking my variables into an array of objects. Check it out--which is easier to understand and is less likely to be buggy:
private struct ControlState{public int myIndex; public bool selected; public object baseState}
-- or --
object [] stateArray { myIndex, selected, baseState }

With a state array, you may get your states out of order, which can never happen with a strongly typed struct. Plus its got an obvious flag that says "hey, don't forget about the base state!", and you can do initialization of variables etc etc etc.

posted by HaackedHaacked(5090) 2 years, 10 months ago

Interesting! I like that approach. The reason I use an array is because it appears to be the convention. If you follow the convention, it just works.

However, with your approach, if I follow the convention, I'll get a runtime error. But, I'll see the BaseState property and hopefully figure it out and do it the right way. That's a real nice benefit of your approach. It's strongly typed and forces me to do the right thing. Well done!

information Login or create an account to comment on this story