mercredi 19 juin 2019

Dynamically-created table loses rows on postback

I have a table with just a TableHeaderRow made up of 5 columns (cells). The first cell/column has a checkbox that I want to be a "check all" for the dynamically created rows in the code-behind.

Table:

<asp:Table runat="server" ID="tblReports" CssClass="table table-condensed table-bordered">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell CssClass="text-center">
                            <asp:UpdatePanel runat="server" ID="updatePanelCheckAllReports" ChildrenAsTriggers="true">
                                <ContentTemplate>
                                    <asp:CheckBox runat="server" ID="cbCheckAllReports" OnCheckedChanged="cb_CheckedChanged" AutoPostBack="true" />
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </asp:TableHeaderCell>
                        <asp:TableHeaderCell>
                        </asp:TableHeaderCell>
                        <asp:TableHeaderCell CssClass="text-center">
                                <h4>Reports</h4>
                        </asp:TableHeaderCell>
                        <asp:TableHeaderCell CssClass="text-center">
                                <h5>Oldest<br />ONLINE<br />ArchiveDate</h5>
                        </asp:TableHeaderCell>
                        <asp:TableHeaderCell CssClass="text-center">
                                <h5>Newest<br />OFFLINE<br />ArchiveDate</h5>
                        </asp:TableHeaderCell>
                    </asp:TableHeaderRow>
                </asp:Table>

Build_Reports_Table:

using (DBContext dc = new DBContext())
            {
                List<string> reports = dc.Database.SqlQuery<string>("SELECT name FROM sys.tables WHERE name like 'report%history' ORDER BY name").ToList();

                foreach (string report in reports)
                {
                    TableRow tableRow = new TableRow();
                    TableCell tableCellCheckBox = new TableCell();
                    TableCell tableCellLabel = new TableCell();
                    CheckBox checkBox = new CheckBox();
                    Label label = new Label();
                    checkBox.ID = "cb" + report;
                    tableCellCheckBox.Controls.Add(checkBox);
                    label.Text = report;
                    tableCellLabel.Controls.Add(label);
                    tableRow.Cells.Add(new TableCell());
                    tableRow.Cells.Add(tableCellCheckBox);
                    tableRow.Cells.Add(tableCellLabel);
                    tableRow.Cells.Add(new TableCell());
                    tableRow.Cells.Add(new TableCell());
                    tblReports.Rows.Add(tableRow);
                }
            }

cb_CheckedChanged method:

CheckBox checkBox = (CheckBox)sender;
            if (checkBox.ID == "cbCheckAllReports")
            {
                if (checkBox.Checked)
                {
                    foreach (TableRow tableRow in tblReports.Rows)
                    {
                        if (!(tableRow is TableHeaderRow) && !(tableRow is TableFooterRow))
                        {
                            CheckBox cbReport = (CheckBox)tableRow.Cells[1].Controls[1];
                            cbReport.Checked = true;
                        }
                    }
                }
                else
                {
                    foreach (TableRow tableRow in tblReports.Rows)
                    {
                        if (!(tableRow is TableHeaderRow) && !(tableRow is TableFooterRow))
                        {
                            CheckBox cbReport = (CheckBox)tableRow.Cells[1].Controls[1];
                            cbReport.Checked = false;
                        }
                    }
                }
            }

The table builds fine after Build_Reports_Table is ran in the Page_Load.

The problem is, for cb_CheckedChanged to trigger the "check all" checkbox has to have AutoPostBack = true. With that set, when the page posts back the table loses all those dynamically added rows and all the table has is the static TableHeaderRow.

I've tried putting an UpdatePanel around the table, that didn't work. So then I put the UpdatePanel around the Checkbox control and that didn't work. Both times when I debugged the cb_CheckedChanged function tblReports.Rows only had the static TableHeaderRow row.

Update

There is a ScriptManager on the master page




Aucun commentaire:

Enregistrer un commentaire