Bad ViewState values for dynamically added checkbox

E

eSapient

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.
 
E

eSapient

Hmnt,

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:
http://members.cox.net/esapient/JobQueue.ASPx
http://members.cox.net/esapient/JobQueue.ASPx.CS

Thanks in advance again.
 
E

eSapient

Hmnt,

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.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top