469,646 Members | 1,145 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Bad ViewState values for dynamically added checkbox

I created a web page which contains a form which contains a table. The
number of rows for the table are determined dynamically. The first cell of
each row contains a HyperLink control and the last cell contains a CheckBox
control. In order for the ViewState mechanism to work correctly, I:

1. create the structure (add rows/columns) of the table in the OnInit
handler when it is a PostBack,
2. create the table structure and populate its cells in the page's Load
handler when it is not a PostBack.

The page works correctly most of the time.

I have two questions:

1. Inside OnInit on a PostBack, where I create the table structure, I need
to know the number of rows to be added to the table. However, per my
understanding, in OnInit, the ViewState has not been parsed, so I cannot use
a ViewState value like ViewState["RowCount"]. So I am forced to use
Session["RowCount"], but don't like it. Is there a better way? I am
presuming using a HtmlInputHidden control would suffer from the same problem
of unavailable ViewState values. I prefer not to use cookies.

2. Sometimes, the ViewState values are not correct. I have a button in the
form which deletes rows from the table. In my test, I have three rows. I
click on the button to remove one row and that is successful. I repeat again
successfully with another row. When there is only one row left in the table
and I select its CheckBox, on PostBack, the CheckBox shows as unchecked. As
a result, the row does not get deleted. Sometimes, not just when there is
one row, I see bad CheckBox states on postback, though it occurs less often.
Any ideas about the cause of this problem?

Thanks in advance.
Nov 18 '05 #1
3 2509

Seems u got a good hold on ViewState ..

Just try out creating yo 'Dynamic' table in 'Page_Load' event ..

Tell me if it doesn't work(Sa*********@hotmail.com)
Nov 18 '05 #2

Thank you very much for your help.

You asked me to try to create the dynamic table in the
page load. However, that is what I tried right at the
beginning and ran into problems because ViewState had
already been parsed before Page_Load, so creating
controls inside Page_Load did not help in populating the
data in the dynamically added rows. I think I may have
not understand you.

What I tried now was to recreate the tables (both, the
structure and the dynamic data contents) at the end of
the handlers for the Click events of the buttons which
modify the table data (including one button which deletes
rows). I thought this would be the right thing to do
since I would be sending back a refreshed snapshot of the
database. However, I continue to see bad values of the
checkboxes' Checked property values on PostBack.

Here are links to the code of the page I created:

Thanks in advance again.
Nov 18 '05 #3

I saw that in another thread ("Kill ViewState !!!") you
had mentioned that Request.Form could be use to retrieve
control values on a PostBack. I checked and found that
that is true. I plan to use that instead of relying on
ViewState. Thanks.
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by John Kirksey | last post: by
6 posts views Thread by Patrick.O.Ige | last post: by
6 posts views Thread by hitendra15 | last post: by
2 posts views Thread by Jarod | last post: by
1 post views Thread by jelle.huygen | last post: by
2 posts views Thread by Dica | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.