C#如何遍历ViewState存储的动态表格并提取单元格值
Let's walk through how to properly access each cell's value from your dynamic table, starting with ensuring the table is correctly stored in ViewState and then extracting the values when needed (like in your btnGenerateEmail click event).
Step 1: Build and Save the Dynamic Table to ViewState
First, make sure your table creation logic in Button1_Click properly generates rows/cells, adds input controls, and saves the table to ViewState. Here's a refined version of that method:
protected void Button1_Click(object sender, EventArgs e) { int rowCount; // Validate the number of rows input if (!int.TryParse(txtNoSitesDeployed.Text, out rowCount) || rowCount <= 0) { // Optional: Show an error message for invalid input return; } Table dynamicTable = new Table(); dynamicTable.ID = "dt"; for (int i = 0; i < rowCount; i++) { TableRow row = new TableRow(); // Add first cell with a TextBox TableCell cell1 = new TableCell(); TextBox txtCell1 = new TextBox { ID = $"txtRow{i}Cell1" }; cell1.Controls.Add(txtCell1); row.Cells.Add(cell1); // Add second cell with a TextBox TableCell cell2 = new TableCell(); TextBox txtCell2 = new TextBox { ID = $"txtRow{i}Cell2" }; cell2.Controls.Add(txtCell2); row.Cells.Add(cell2); dynamicTable.Rows.Add(row); } // Replace the existing asp:Table's content with the dynamic rows dt.Controls.Clear(); foreach (TableRow row in dynamicTable.Rows) { dt.Rows.Add(row); } // Save the full table to ViewState for later retrieval ViewState["DynamicTable"] = dynamicTable; }
Step 2: Extract Cell Values in the Email Generation Button Click
To get the user-inputted values from each cell, retrieve the table from ViewState and loop through each row. Since your cells contain input controls (like TextBoxes), you'll need to access those controls directly to get their values:
protected void btnGenerateEmail_Click(object sender, EventArgs e) { // Check if the table exists in ViewState if (ViewState["DynamicTable"] is Table dynamicTable) { foreach (TableRow row in dynamicTable.Rows) { // Ensure the row has at least 2 cells before accessing if (row.Cells.Count >= 2) { // Get value from the first cell's TextBox if (row.Cells[0].Controls.Count > 0 && row.Cells[0].Controls[0] is TextBox txt1) { string cell1Value = txt1.Text; // Use this value (e.g., append to email body) } // Get value from the second cell's TextBox if (row.Cells[1].Controls.Count > 0 && row.Cells[1].Controls[0] is TextBox txt2) { string cell2Value = txt2.Text; // Use this value as needed } } } } }
Critical Note: Preserve Dynamic Controls on Postback
Dynamic controls (like the TextBoxes in your table) need to be recreated on every postback to retain their state. Add this logic to Page_Load to reattach the table from ViewState whenever the page posts back:
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack && ViewState["DynamicTable"] is Table dynamicTable) { dt.Controls.Clear(); foreach (TableRow row in dynamicTable.Rows) { dt.Rows.Add(row); } } }
This ensures that when you click btnGenerateEmail, all the table's controls are present and their values are accessible.
内容的提问来源于stack exchange,提问作者Robert

