ASP.NET - how to disable viewstate for dynamically created table

Asked By anbu n on 31-Oct-11 12:26 PM
how to disable viewstate for dynamically created table,

here is code for creating dynamic table,

private void CreateDynamicTable()      
    {
      try
      {

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


        // Fetch the number of Rows and Columns for the table
        // using the properties
       // int tblRows = Rows;
       // int tblCols = Columns;

        // 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
        

        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 = "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;" + "" + (j+1) +"";             

              tc.ID = "dy" + i + "," + j;
              tc.Controls.Add(lbl_1);
              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 = "";

              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.pnlPopup.FindControl("PlaceHolder1").Controls.Add(tbl);
         


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

        ViewState["dynamictable"] = true;
      }
      catch (Exception ex)
      {
      
      
      }
    }

-----------------------------------------

here each time the table is generated , the older values which enterd before are in the textboxes but not visible..... its added like 5252,,,555,,.

how to clear this older values


Devil Scorpio replied to anbu n on 31-Oct-11 03:01 PM

Hi

try this

The following code example demonstrates how to do this: <%@ Control Language="C#" ClassName="Test" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlTableRow row = new HtmlTableRow();
        HtmlTableCell cell = new HtmlTableCell();
        TextBox text = new TextBox();
        text.ID = "test";
        cell.Controls.Add(text);
        row.Cells.Add(cell);
        table1.Rows.Add(row);
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (ViewState["text"] != null)
        {
            Response.Write("ViewState Value:" + ViewState["text"]);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["text"] = ((TextBox)this.FindControl("test")).Text;
    }

</script>

<table id="table1" runat="server"  border="1">
        
     </table> 
     <asp:Button ID="Button1" runat="server" Text="Get ViewState" 
         onclick="Button1_Click" />   
           

Jitendra Faye replied to anbu n on 01-Nov-11 02:19 AM
The following server controls persist their information across requests even when the control ViewState (the EnableViewState attribute) is set to False:
  • The TextBox control.
  • The CheckBox control.
  • The RadioButton control.


This behavior occurs because the ViewState of a control is only one of the methods that are used to persist a control's attributes across requests. In the server controls that are mentioned in the "Symptoms" section, attributes that are not normally posted to the server through the form-get or the form-post are handled by the ViewState.

follow this link here you will get article about this-

http://support.microsoft.com/?id=316813