Checked checkbox says not checked
-
Hello, I dynamically create a check box in each row of a table. (dynamically created rows) When I click a button to check the status of the check boxes, they all come back a not checked. This is how I add the check boxes ----- cell = New TableCell cell.Controls.Add(chkHeaderDelete_Array(HeaderLoop)) row.Cells.Add(cell) tblHeader.Rows.Add(row) ----- and I check them by ----- rowCtr = tblHeader.Rows.Count For ctr = 0 To rowCtr - 1 row = tblHeader.Rows(ctr) chkBox = row.Cells(0).Controls(0) If chkBox.Checked Then .... ----- And every check box in the table will come back as not checked. Is there anything I can do to make it properly recognize when check box is checked or not? Thanks The wisest of the wise may err. - Aeschylus
-
Hello, I dynamically create a check box in each row of a table. (dynamically created rows) When I click a button to check the status of the check boxes, they all come back a not checked. This is how I add the check boxes ----- cell = New TableCell cell.Controls.Add(chkHeaderDelete_Array(HeaderLoop)) row.Cells.Add(cell) tblHeader.Rows.Add(row) ----- and I check them by ----- rowCtr = tblHeader.Rows.Count For ctr = 0 To rowCtr - 1 row = tblHeader.Rows(ctr) chkBox = row.Cells(0).Controls(0) If chkBox.Checked Then .... ----- And every check box in the table will come back as not checked. Is there anything I can do to make it properly recognize when check box is checked or not? Thanks The wisest of the wise may err. - Aeschylus
Basically, the problem is this. You dynamically create a checkbox, then your page posts back. After PageInit, but before PageLoad, your viewstate is restored, and the values stored in it are mapped to controls on the page. If your controls do not exist yet, then the viewstate for those controls are lost. You should override LoadViewState and before calling it, you should create your checkboxes there. They need to be in the same place, with the same name, every time, and then the viewstate will be restored. Christian Graus - Microsoft MVP - C++
-
Basically, the problem is this. You dynamically create a checkbox, then your page posts back. After PageInit, but before PageLoad, your viewstate is restored, and the values stored in it are mapped to controls on the page. If your controls do not exist yet, then the viewstate for those controls are lost. You should override LoadViewState and before calling it, you should create your checkboxes there. They need to be in the same place, with the same name, every time, and then the viewstate will be restored. Christian Graus - Microsoft MVP - C++
-
Thank you. It works now thanks to your advice. The wisest of the wise may err. - Aeschylus
Glad to help - it's definately one of the most tricky areas to get right in ASP.NET. :-) Christian Graus - Microsoft MVP - C++