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.