ViewState Question

T

Tony

I am creating a table dynamicly with some edit controls in each cell. I added
a save button to my form. When the save button is processed a round trip is
made to the server and I loose the contents of the table.

How do I not loose the controls that make up the table
TIA
 
T

Tony

That's crazy - did MS just forget to implement anything :-(

If I've got a table with 30 edit controls all created dynamically I've got
to create hanlders for each one and constantly keep track of any changes. In
ASP I could create HTML and keep control of my data but in ASP.NET I can't

Thanks for the reply though
 
D

Drew Robbins

The trick is to recreate the control tree during the Initialization
(OnInit) phase of page execution. If you recreate the same control tree
and make sure the ID is the same for each control in your table between
requests, then ASP.NET can properly process postback events and viewstate.
 
R

Robert Koritnik

Waht are so bit**in' about? As you said, in ASP you had create your HTML.
Also every time a request came to the server. In ASP.NET things are the same
if we consider this, but you have much more control and power over this. If
you would think a bit how ASP.NET works (or IIS or any web server), this
kind of thinking would be self-explainable its a NO GO. How should server
preserve controls? In memory? for 100.000 users that access this
application? Hmmm. I don't think so. Persisting on client side (in any of
possible ways)? Hmmm. Too much data to transfer back and forth... Hmmm. How
else? Database? Server speed limitations... What else could we do?...

ASP.NET saves page state, so data and everything is maintained when posting
back. You have to take care that controls will be created at the given time,
so ASP.NET will be able to work with. Isn't this somehow the same as it was
in the old ASP days?...

Figures...

But beside this. Anyway you look at it, yes every technology has flaws, so
does ASP.NET but it don't think this behaviour is a flaw. At least it looks
quite smart to me.
 
S

Scott Mitchell [MVP]

Tony said:
I am creating a table dynamicly with some edit controls in each cell. I added
a save button to my form. When the save button is processed a round trip is
made to the server and I loose the contents of the table.

How do I not loose the controls that make up the table

Tony, make sure you are recreating your control structure on each page
load. I discuss this in the following three articles:

Dynamic Controls in ASP.NET
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

Working with Dynamically Created Controls
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx

Dynamic Web Controls, Postbacks, and View State
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx


These three articles ought to get you off on the right step...

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
T

Tony

Thanks Scott and Drew - I have it all working now :)
Creating the controls in On_Init( ) did the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top