views:

187

answers:

1

I have a control (say, a DataGrid or a ComboBox) which is a child of a user control. I want to DataBind it on every request, rather than have it's state persist through ViewState. I need to leave EnableViewState=true however. This means, I think, that I need to call DataBind before TrackViewState() is called.

I read the fantastic blog post TRULY Understanding ViewState and it answers my question in 4. Initializing child controls programmatically. However, the article's solutions are less-than-ideal: create child controls dynamically (I'm not and can't -- too much existing code), use a 3rd party CodeExpressionBuilder (would much prefer this was in code-behind), and use OnPreInit (which "doesn't help you what-so-ever if you are developing a CONTROL").

Since that article is rather old (circa 2006) I was hoping that newer versions of ASP.NET rectify the situation such that there are better solutions now. So, community, are there any GOOD ways to do this?

A: 

Scott,

I think there's a misunderstanding of how ASP.NET Web Forms framework works. So just a little theory first.

ASP.NET Web Forms pages have design and code-behind files. Design file has HTML markup and "controls" which are reference tags to either user or custom controls. Every element that has attribute runat = "server" is server control (meaning it is accessible from the code behind by its ID).

When page is opened in the browser, ASP.NET takes design file and processes these control references and finally generates pure HTML.

ViewState is a hidden variable in HTML that keeps internal state of all controls that are marked with runat = "server" and also have property EnableViewState set to True.

Say you have DataGrid with EnableViewState = False. This grid has rows that contain server controls and you want them to be in ViewState. You still can have them have EnableViewState = True and be in ViewState and you still can bind to events for these sub-controls.

So even though DataGrid is not in ViewState, its children can be in ViewState and you can handle appropriate events.

You will need to initialize DataGrid in the PageLoad event every time if you want it to show data after every postback.

ViewState keeps internal state of control, so you can't have some properties of control in ViewState and others not in ViewState. But you can have child controls (they're not properties, they're subentities!) to be in ViewState even though parent control is not in ViewState.

Let me know if you want me to be more specific on this. If you want to go deep inside there's a brilliant book from Nikhil Kothari about developing controls. I think I saw it somewhere as e-book.

Vitaly
Thank you for a good summary of ViewState, but I don't believe that it answers my question. Read the article I referenced, if you haven't already, especially the section I mentioned, and I think you'll see what i mean.
Scott Stafford
Scott, could you tell what questions do you still have after my explanation?
Vitaly