ASP.NET - Clear dynamically created table with enabled viewstate

Asked By anbu n on 02-Nov-11 05:23 AM
private void CreateDynamicTable()      
    {
      try
      {

         this.pnlPopup.FindControl("PlaceHolder1").Controls.Clear();        


      
        // Create a Table and set its properties


        Table tbl = new Table();
        tbl.ID = "tblDynamic";        
        tbl.BorderWidth = 1;
        tbl.GridLines = (GridLines)3; // no. 3 means --> GridLines="Both"
        tbl.Attributes["runat"] = "server";        
        
        // Add the table to the placeholder control
        this.pnlPopup.FindControl("PlaceHolder1").Controls.Add(tbl);

        for (int i = 0; i < 4; i++)
        {
          TableRow tr = new TableRow();
          TableCell tc_1 = new TableCell();
          Label lbl = new Label();


         // System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#ffcc00");

         // lbl.ForeColor = c;


          if (i == 0)
          {
            tc_1.ID  ="dyn"+ i + "," + 0;
            lbl.ID = "lblRow_" + i + "Col_" + 00;
            lbl.Text = "CapEx";
          }
          else if (i == 1)
          {
            tc_1.ID = "dyn" + i + "," + 0;
            lbl.ID = "lblRow_" + i + "Col_" + 00;
            lbl.Text = "Leasing Costs";
          }
          else if (i == 2)
          {
            tc_1.ID = "dyn" + i + "," + 0;
            lbl.ID = "lblRow_" + i + "Col_" + 00;
            lbl.Text = "Capital Reserves";
          }
          else if (i == 3)
          {
            tc_1.ID = "dyn" + i + "," + 0;
            lbl.ID = "lblRow_" + i + "Col_" + 00;
            lbl.Text = "Total CapEx";
          }

          tc_1.Controls.Add(lbl);
          tr.Cells.Add(tc_1);

          for (int j = 0; j < Convert.ToInt32(txtValperiod.Text); j++)
          {
            TableCell tc = new TableCell();
            //TableCell tc_2 = new TableCell();

            TextBox txtBox = new TextBox();
            Label lbl_1 = new Label();


            if (i != 3)
            {
              txtBox.Attributes.Add("onblur", "javascript:AddCapEXrow(this)");
            }
            //System.Drawing.Color c1 = System.Drawing.ColorTranslator.FromHtml("#ffcc00");

           // lbl_1.ForeColor = c1;


            if (i == 0 && j ==0)
            {
              lbl_1.Text = "&nbsp;" + "&nbsp;" + "year" + "&nbsp;" + (j + 1);

              tc.ID = "dy" + i + "," + j;
              tc.Controls.Add(lbl_1);
              tr.Cells.Add(tc);

            }
            else if (i == 0 && j != 0)
            {
              lbl_1.Text = "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "" + (j + 1) + "";             

              tc.ID = "dy" + i + "," + j;
              tc.Controls.Add(lbl_1);
              tr.Cells.Add(tc);

            }
            else if (i == 3 )
            {
              // Set a unique ID for each TextBox added

              txtBox.ID = "TextBoxRow_" + i + "Col_" + j;
              txtBox.Width = 50;
              txtBox.EnableViewState = false ;
              //txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;

              txtBox.Text = stringFurnisher_dynamic(txtBox.Text);
              
              txtBox.Text = "";         

              tc.ID = "dy" + i + "," + j;
              // Add the control to the TableCell
              tc.Controls.Add(txtBox);

              // Add the TableCell to the TableRow
              tr.Cells.Add(tc);
            }
            else
            {
              // Set a unique ID for each TextBox added

              txtBox.ID = "TextBoxRow_" + i + "Col_" + j;
              txtBox.Width = 50;
              txtBox.EnableViewState = false;
              //txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;

              txtBox.Text = stringFurnisher_dynamic(txtBox.Text);

              txtBox.Text = "0";

              tc.ID = "dy" + i + "," + j;
              // Add the control to the TableCell
              tc.Controls.Add(txtBox);

              // Add the TableCell to the TableRow
              tr.Cells.Add(tc);
            }
            
          }
          // Add the TableRow to the Table
          tbl.Rows.Add(tr);
        }    


        // This parameter helps determine in the LoadViewState event,
        // whether to recreate the dynamic controls or not

        ViewState["dynamictable"] = true;
      }
      
    }
-------------------------------
in above is the code to generate dynamic table with viewstate,

in a button click i need to completely clear the above dynamically created table control

for clearing table control:

1,  this.pnlPopup.FindControl("PlaceHolder1").Controls.Clear();  
    the above code clears , but after clicking some button  the following error occurs,  

Error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. Source File: http://localhost:1949/DPRWEB/ScriptResource.axd?d=_koTdAymFM3rS8_YeQs2PM1E7UcH1ususvSaiR6uVouARVFgLidqOTmmyTAnUyJHMTCmZQHW4cqgwa0KMRcYk9zcTeuZx3hxTGGfihFemF5BBp4de2ca-k22u5cgRZxy0&t=ffffffff96473cf2 Line: 1111

2,  this.PlaceHolder1.Dispose();

this above code does clear , but there are some data s in textboxes in dynamically created control which can't been seen


-------------
i require to clear the above dynamically created table to state how it is when the page first loads




 







dipa ahuja replied to anbu n on 02-Nov-11 07:04 AM
better to store it in the session instead of ViewState

<table id="t1" runat="server" border="1">
<tr>
<td>
  <asp:Label ID="Label1"  runat="server" Text="dipa"></asp:Label>
  <asp:Label ID="Label2" runat="server" Text="sita"></asp:Label>
</td>
</tr>
</table>

Session["t1"] = t1;

Next Page:

HtmlTable t1 = (HtmlTable)Session["t1"];
Panel1.Controls.Add(t1);