views:

1204

answers:

5

On my journey into the depths of custom ASP.NET control development I am obviously getting my head around the ASP.NET PostBack model and how it affects control development.

I understand that controls have no "lifetime" in ASP.NET, and therefore must be re-initialized on each and every page load. We overcome this by persisting the objects values/parameters to the ViewState.

Many articles I read therefore suggest not using PostBack since this can add considerable overhead to the Page. I am not looking for how to disable it, I know that.

What I am looking for is:

What alternatives to we have to using the PostBack model to initialize controls?

I know we could use the QueryString, but that seems awfully messy, and obviously unreliable.

Ideally you could give me an overview of the architecture/design of a different approach and the pro's/con's of it..

Many thanks ^_^

+2  A: 

Well, Session State is a server-side solution, with its own pile of cruft to deal with if you want to avoid ViewState altogether. Really though, using ViewState in a custom control is all fine and good - just be picky about what you store - only store deltas from the declared control state, don't store anything you're going to get on postback anyway (e.g. from a DB call), etc.

Greg Hurlman
I think you make an informed yet neutral answer, exactly what I wanted. I am continuing to mess with making controls so no doubt I will be back with more Q's in the future. Im slowly getting my head around using ViewState judiciously, coming from WinForms its hard getting used to the statelessness!
Rob Cooper
+1  A: 

You have to store the values somewhere, so you are limited to the query string and hidden form fields. If you relate that to HTTP, basically it's either GET or POST parameters.

I suppose you could use cookies, but that would be really messy.

Eric Z Beard
+1  A: 
  1. Store your object state in the session context: this will shift the burden of keeping state from the client to the server, which may be acceptable for small-scale intranet apps. For sites on the capital-I Internet, this won't work;

  2. AJAX-enable your control: in this case, only state changes need to be posted back. Picking the right framework is key here; see http://www.asp.net/ajax/ajaxcontroltoolkit/samples/ for the official MS approach; many others are possible.

mdb
A: 

I think you still mis-understand controls somewhat. Controls only have the problem you describe when you add them to the page dynamically. If you declare your controls upfront in the aspx code then they build along with the page.

Joel Coehoorn
Are you sure this is correct? I understand that the markup is compiled, but as the controls values change with each request, AFAIK the only place to store such values is the PostBack?
Rob Cooper
Well, thats my point. I have. And I observed (as expected) the control is re-inited on each page request and my changes are lost?
Rob Cooper
Depends on what you mean by 'changes', and how you doing the changes. Control's state should be restored automatically _as long as the control exists before the Load phase._
Joel Coehoorn
Rob - you are correct. The page object is rebuilt with the declared control values as default. Then, the ViewState is laid over top of that, applying changes.
Greg Hurlman
What Joel is describing is the default functionality you get from Control.
Greg Hurlman
Okm say I have MyCtl and MyCtl.Val = 1 in the ASPX. I run the app, click a button and make MyCtl.Val = 2, when the page refreshes it appears as 1, unless I save this non-default value in ViewState?
Rob Cooper
Yes. This is what viewstate is for: user changes to input values. And viewstate is fine for this. If you start getting values into the viewstate set by the application that you have a problem.
Joel Coehoorn
So back to the question, I am looking for alternative methods to using the PostBack model :)
Rob Cooper
+1  A: 

If you're truly looking for alternatives to the PostBack model altogether, then I would suggest researching the ASP.NET MVC Framework. I would love to kick WebForms to the curb and do all my stuff in MVC, but alas, legacy code is a tarbaby and rewriting is almost never the answer, so I plug onwards...

sliderhouserules
Something I really, really want to do, but sadly, work are not yet moving on up to MVC :( Something I intend to work hard on changing next year :D We still have apps in ASP classic :(
Rob Cooper
My last job I had a hybrid ASP/ASP.NET site, and I was trying to start doing stuff in MVC. MVC can coexist with WebForms easily. The routing engine will route MVC URLs, and pass on any that it doesn't match. Could be wrong as I didn't get to do it, but I'm pretty sure it works.
sliderhouserules