2013-01-09 78 views
5

之間存留着ASPX代碼隱藏的ASP.NET動態生成TableRows不會回發

<asp:Table ID="superTable" runat="server" Width="100%"> 
    <%--populate me on the fly!--%> 
</asp:Table> 

<asp:Button ID="btnAddRow" runat="server" CausesValidation="false" Text="Add Row" onclick="btnAddRow_Click" Width="90%"/> 

<asp:Button ID="btnRemoveRow" runat="server" CausesValidation="false" Text="Remove Last Row" onclick="btnRemoveRow_Click" Width="90%"/> 

<asp:Button ID="btnSubmit" runat="server" Text="1" onclick="btnSubmit_Click" Width="90%"/> 

相關位

protected void Page_Load(object sender, EventArgs e) 
    {if (!IsPostBack){ writeHeader(); makeMeARow(); }} 

protected void btnAddRow_Click(object sender, EventArgs e) 
{ 
    if (int.Parse(btnSubmit.Text) <= 20) 
    { int b = superTable.Rows.Count+1; 

     writeHeader(); 
     btnSubmit.Text = (int.Parse(btnSubmit.Text) + 1).ToString(); 

     for (int a = 1; a <= int.Parse(btnSubmit.Text); a++) 
      { makeMeARow(); } 
    } 
    else{/*tell user they can't do that! Max of 20 rows as noted by form requirements */} 
} 

private void writeHeader() 
{ 
    //= == create row == =// 
    TableHeaderRow tempHeaderRow = new TableHeaderRow();//make row 

    //= == create cells == =// 
    TableHeaderCell tempHeaderCell01 = new TableHeaderCell(); 
    TableHeaderCell tempHeaderCell02 = new TableHeaderCell(); 
    TableHeaderCell tempHeaderCell03 = new TableHeaderCell(); 

    tempHeaderCell01.Text = "Call Number"; tempHeaderCell01.Width = Unit.Percentage(33); 
    tempHeaderCell02.Text = "Author";  tempHeaderCell02.Width = Unit.Percentage(33); 
    tempHeaderCell03.Text = "Title";  tempHeaderCell03.Width = Unit.Percentage(33); 

    //= == add TableCells to TableRow == =// 
    tempHeaderRow.Cells.Add(tempHeaderCell01); 
    tempHeaderRow.Cells.Add(tempHeaderCell02); 
    tempHeaderRow.Cells.Add(tempHeaderCell03); 

    //superTable.Rows.AddAt(superTable.Rows.Count, tempRow); 
    superTable.Rows.Add(tempHeaderRow); 
} 

protected void btnRemoveRow_Click(object sender, EventArgs e) 
{ int b = superTable.Rows.Count - 1; 

    writeHeader(); 
    btnSubmit.Text = (int.Parse(btnSubmit.Text) - 1).ToString(); 
    for (int a = 1; a <= int.Parse(btnSubmit.Text); a++) 
    {makeMeARow();} 
} 
private void makeMeARow() 
{ 
    //= == maybe off by one? == =// 
    string rowCount = superTable.Rows.Count.ToString("00"); 

    //= == create row == =// 
    TableRow tempRow = new TableRow();//make row 

    //= == create cells == =// 
    TableCell tempCell01 = new TableCell(); 
    TableCell tempCell02 = new TableCell(); 
    TableCell tempCell03 = new TableCell(); 

    //= == create TextBoxes == =// 
    TextBox tempTextBox01 = new TextBox(); 
    TextBox tempTextBox02 = new TextBox(); 
    TextBox tempTextBox03 = new TextBox(); 

    //= == change the ID of TableRow == =// 
    tempRow.ID = "tableRow_" + rowCount; 

    //= == change the IDs of TableCells == =// 
    tempCell01.ID = "tableCell_" + rowCount + "_01"; 
    tempCell02.ID = "tableCell_" + rowCount + "_02"; 
    tempCell03.ID = "tableCell_" + rowCount + "_03"; 

    //= == change the IDs of TextBoxes == =// 
    tempTextBox01.ID = "txtCallNumber_" + rowCount; 
    tempTextBox02.ID = "txtAuthor_" + rowCount; 
    tempTextBox03.ID = "txtTitle_" + rowCount; 

    //= == change TextBox widths to 90%; 
    tempTextBox01.Width = Unit.Percentage(90); 
    tempTextBox02.Width = Unit.Percentage(90); 
    tempTextBox03.Width = Unit.Percentage(90); 

    //= == add TextBoxes to TableCells == =// 
    tempCell01.Controls.Add(tempTextBox01); 
    tempCell02.Controls.Add(tempTextBox02); 
    tempCell03.Controls.Add(tempTextBox03); 

    //= == add TableCells to TableRow == =// 
    tempRow.Cells.Add(tempCell01); 
    tempRow.Cells.Add(tempCell02); 
    tempRow.Cells.Add(tempCell03); 

    //add TableRow to superTable 
    //superTable.Rows.AddAt(superTable.Rows.Count, tempRow); 
    superTable.Rows.Add(tempRow); 
} 

好了,所以,我的問題; - 當我點擊「添加行」或「刪除行」按鈕時,單元格中的數據不會在回發之間持續存在。相關的行和單元格具有相同的ID,但不保留數據。爲什麼不?

+0

是否啓用了ViewState? – n8wrl

+0

不知道 - 我該怎麼做,在哪裏做? – statue

+0

我不認爲ASP:表應該堅持其內容在ViewState,但我不完全確定。通常在股票ASP.NET中,您應該在每次回發中恢復所有動態控件。如果服務器端沒有足夠的數據,則可以在ViewState中添加其他信息。 – Zarat

回答

10

動態控件必須重新添加到每個回發上的表單。通常這是在頁面生命週期的Init階段完成的。您動態添加的控件實際上有ViewState。當適當的控件重新添加到控件樹時,使用與之前完全相同的ID,它應該重新顯示在ViewState中保留的值。

請查看this article瞭解使用動態控件的簡單提示,或者您可以從4 Guys from Rolla查看本教程以獲得更深入的瞭解。