gpt4 book ai didi

c# - 在动态创建的表中获取动态控件值

转载 作者:行者123 更新时间:2023-11-30 22:18:09 25 4
gpt4 key购买 nike

我正在尝试读取表格单元格中的复选框值,但是在通过按钮提交进行回发时,整个表格都消失了。我只在发生 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_Load() 之前访问的 page_Init() 中进行重新创建。

关于c# - 在动态创建的表中获取动态控件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com