clamum
Lifer
I am dynamically creating table rows, cells, and controls (dropdown lists and checkboxes) in ASP.NET. I have a standard "<table id='tblName' runat='server'></table>" tag in the aspx and in the code-behind I dynamically create rows depending on a count. In those rows are two columns, one for a dropdown list and one for a checkbox. In a final dynamic row, there is a button being created that adds a new row.
My problem is that I'll first add a new row (the table starts out blank except for the row with the button), select an item in the dropdown list and/or check the checkbox, and then click the button to add another row. Clicking the button adds the new row fine, but the dropdown list and checkbox in the first row are then reset.
I thought the values of controls were stored in the ViewState but perhaps I'm misunderstanding. I should also note that this is being done in an UpdatePanel to avoid full page postbacks. Does anyone have any insight as to what's going on and why the values do not propagate across button clicks? Thanks!
The code is roughly:
C#:
void Page_Load() {
....int[] listOfRowNumbers = GetFromDB();
....BuildTable();
}
void BuildTable() {
....foreach (int i in listOfRowNumbers) {
........HtmlTableRow row = new HtmlTableRow();
........// create cell here
........DropDownList ddl = new DropDownList();
........ddl.DataSource = someCollection();
........ddl.DataBind();
........// create checkbox
........// create button, assign click handler
........// add controls to cell, cell to row, row to table
....}
}
void Button_Click() {
....// add new row number to list of row numbers, save to db
....BuildTable();
ASPX:
<asp:UpdatePanel>
....<ContentTemplate>
........<table id="tblTable" runat="server"></table>
....</ContentTemplate>
</asp:UpdatePanel>
My problem is that I'll first add a new row (the table starts out blank except for the row with the button), select an item in the dropdown list and/or check the checkbox, and then click the button to add another row. Clicking the button adds the new row fine, but the dropdown list and checkbox in the first row are then reset.
I thought the values of controls were stored in the ViewState but perhaps I'm misunderstanding. I should also note that this is being done in an UpdatePanel to avoid full page postbacks. Does anyone have any insight as to what's going on and why the values do not propagate across button clicks? Thanks!
The code is roughly:
C#:
void Page_Load() {
....int[] listOfRowNumbers = GetFromDB();
....BuildTable();
}
void BuildTable() {
....foreach (int i in listOfRowNumbers) {
........HtmlTableRow row = new HtmlTableRow();
........// create cell here
........DropDownList ddl = new DropDownList();
........ddl.DataSource = someCollection();
........ddl.DataBind();
........// create checkbox
........// create button, assign click handler
........// add controls to cell, cell to row, row to table
....}
}
void Button_Click() {
....// add new row number to list of row numbers, save to db
....BuildTable();
ASPX:
<asp:UpdatePanel>
....<ContentTemplate>
........<table id="tblTable" runat="server"></table>
....</ContentTemplate>
</asp:UpdatePanel>