How to avoid saving data via ViewState for a child control in ASP.NET?

I have a control (say, DataGrid or ComboBox) that is a child of a user control. I want a DataBind for each request, instead of saving its state through the ViewState. I need to leave EnableViewState=true. This means that I think I need to call DataBind before calling TrackViewState ().

I read a fantastic TRULY blog post Understanding ViewState and it answers my question at 4. Initializing child controls programmatically. However, the solutions for articles are less than ideal: dynamically create child controls (I cannot and cannot - existing code is too large), use third-party CodeExpressionBuilder (I would like it to be in the code), and use OnPreInit (which " doesn't help you ever if you develop CONTROL ").

Since this article is quite old (circa 2006), I was hoping that newer versions of ASP.NET would fix the situation so that now there are better solutions. So community, are there any good ways to do this?

+3
source share
1 answer

Scott

I think there is a misunderstanding of how ASP.NET Web Forms works. So, first a little theory.

ASP.NET Web Forms pages have design and code files. The project file has HTML markup and “controls”, which are link tags for custom or custom controls. Each element that has an attribute runat = "server"is a server (this means that it is accessible from the code by its identifier).

When the page opens in a browser, ASP.NET accepts the design file and processes these control links and finally generates pure HTML.

ViewState - HTML, , runat = "server", EnableViewState, True.

, DataGrid EnableViewState = False. , , , ViewState. EnableViewState = True ViewState, - .

, DataGrid ViewState, ViewState, .

DataGrid PageLoad , , .

ViewState , ViewState, ViewState. ( , !), ViewState, ViewState.

, , . , . , - .

0

Source: https://habr.com/ru/post/1735306/


All Articles