2013-05-02 86 views
1

我想读取我的表格单元格中的复选框值,但在通过按钮提交进行回发时,整个表格消失。我只创建表,如果它不是一个发布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(); 
    } 
} 

回答

2

我想你应该在page_Init()中调用你的方法CreateTable()。 ,因为在每个回帖后面,您创建的每个DOM内容都将消失。所以你必须在每个帖子后面重新创建它,所以你必须在page_Init()中做这个重载,这个重载在page_Load()之前被访问。