2

我正在尝试读取表格单元格中的复选框值,但是在通过按钮提交进行回发时,整个表格消失了。如果在 page_load 发生时它不是回发,我只创建该表,并且我认为该表一旦创建就会在回发中持续存在。

如何保留整个表格及其单元格的复选框值?谢谢。

protected void CreateTable()
{
    int rowCnt; // Total number of rows.
    int rowCtr; // Current row count.
    int cellCtr; // Total number of cells per row (columns).
    int cellCnt; // Current cell counter.

    rowCnt = 6;
    cellCnt = 8;

    string baseStartTime = (ConfigurationManager.AppSettings["DEFAULTBASESELLSCHEDULETIME"]);
    int incrementInMins = Convert.ToInt32((ConfigurationManager.AppSettings["DEFAULTBASESELLSCHEDULETIME_INCREMENT"]));

    DateTime tempTimeFrom = Convert.ToDateTime(baseStartTime); // Converts only the time
    tempTimeFrom = tempTimeFrom.AddMinutes(-incrementInMins);
    // Because the very first loop will add 30 mins right away 

    for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
    {
        tempTimeFrom = tempTimeFrom.AddMinutes(incrementInMins);
        DateTime tempTimeTo = tempTimeFrom.AddMinutes(incrementInMins);

        string timeFrom = tempTimeFrom.ToString("hh:mm tt");
        string timeToClassName = tempTimeTo.ToString("hh:mm");
        string timeTo = tempTimeTo.ToString("hh:mm tt");

        // Create a new row and add it to the table.
        TableRow tRow = new TableRow();
        tblSellSchedule.Rows.Add(tRow);
        for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
        {
            // Create a new cell and add it to the row.
            TableCell tCell = new TableCell();
            tRow.Cells.Add(tCell);

            if (cellCtr == 1) // We need the time for the first column of every row
            {
                tCell.Controls.Add(new LiteralControl(timeFrom + "-" + timeTo));
                tCell.CssClass = timeToClassName;
            }
            else
            {
                // tCell.Controls.Add(new LiteralControl("Select"));
                CheckBox chkbox = new CheckBox();

                chkbox.ID = tblSellSchedule.Rows[rowCtr - 1].Cells[0].CssClass + (cellCtr - 1);
                tCell.Controls.Add(chkbox);
                //  tCell.ID = (cellCtr - 1).ToString();
                tCell.CssClass = (cellCtr - 1).ToString();
            }
        }
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (TableRow row in tblSellSchedule.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            foreach (CheckBox c in cell.Controls.OfType<CheckBox>())
            {
                if (c.Checked)
                {
                    var idVal = c.ID;
                }
            }
        }
    }
}

protected void Page_Load(object sender, EventArgs e)     
{
    if (!Page.IsPostBack)
    {
        CreateTable();
    }
}
4

1 回答 1

3

我认为你应该在 page_Init() 中调用你的方法 CreateTable()。因为在每次回帖时,您创建的每个 DOM 内容都会消失。所以你必须在每次回帖时重新创建它,所以你必须在 page_Load() 之前访问的 page_Init() 中重新创建它。

于 2013-05-02T05:31:47.357 回答