在动态创建的表中获取动态控件值
本文关键字:获取 动态控件 动态 创建 | 更新日期: 2023-09-27 18:11:58
我试图在我的表格单元格中读取我的复选框值,但是在通过按钮提交进行回发时,整个表消失了。当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();
}
}
我认为你应该在page_Init()中调用你的方法CreateTable()。因为在每次回发时,您创建的所有DOM内容都会消失。所以你必须在每次回发时重新创建它,所以你必须在page_Init()中重新创建它它在page_Load()之前被访问