Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retain values from dynamically created table with controls

Posted on 2004-11-01
4
Medium Priority
?
719 Views
Last Modified: 2008-03-03
I have a WebControls.Table that users are able to Add rows to using an Add button.  When the button is clicked, a new table row is created with several controls.  I am using a session variable to retain an arraylist of my table rows so that I can recreate the table on postback.  My issue is, that although the table is recreated, any data that my user may have typed in is gone - it appears the ViewState is not being maintained.

Here is some of my code:
            private void BtnAddBM_Click(object sender, System.EventArgs e)
            {              
                  //Add row table - get existing rows
                  newRows = (ArrayList)Session["newRows"];

                  //set rcount to initial row id - we have two header rows, so first row is 2
                  int rcount = 2 + newRows.Count;

                  //Create a new row
                  TableRow newRow = new TableRow();
                  newRow.ID = "nr" + rcount.ToString();
                  //Add cells
                  newRow.Cells.Add(new TableCell());
                  newRow.Cells.Add(new TableCell());
                  newRow.Cells.Add(new TableCell());
                                                .
                                                .
                                                .
                  //Add the controls to the new row
                  newRow.Cells[0].Controls.Add(txtBBM);
                  newRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[1].Controls.Add(ddlCurEmpTit);
                  newRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[2].Controls.Add(ddlFETit);
                  newRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[3].Controls.Add(cbAI);
                  newRow.Cells[3].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[4].Controls.Add(cbVC);
                  newRow.Cells[4].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[5].Controls.Add(cbII);
                  newRow.Cells[5].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[6].Controls.Add(cbOI);
                  newRow.Cells[6].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[7].Controls.Add(cbAc);
                  newRow.Cells[7].HorizontalAlign = HorizontalAlign.Center;
                  newRow.Cells[8].Controls.Add(txtBBMOth);
                  newRow.Cells[8].HorizontalAlign = HorizontalAlign.Center;

                  //Store the new row in the array list
                  newRows.Add(newRow);

                  //Store the updated array list in session object
                  Session["newRows"] = newRows;

                  //Add all the rows created dinamicly to the table
                  foreach(TableRow currentRow in newRows)
                  {
                        Table2.Rows.Add(currentRow);
                  }

                                 }

How can I retain the values in these controls between PostBacks?

Thanks,

Dave

0
Comment
Question by:a222493
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 12467169
Remember with dynamic controls, they should be created each time the page loads. And also remember that neither the controls, nor their properties are available on the postback, because page has no way of knowing these controls on the postback, until they are recreated, that means the client changes are gone.
0
 
LVL 8

Expert Comment

by:nishikanth
ID: 12471095
the problem with dynamica controls is that you must recreate them at every post back....

everyone faces this problem with Dynamic controls


http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
http://www.c-sharpcorner.com/Code/2003/July/DynamicallyCreateASPNETControls.asp
0
 
LVL 8

Accepted Solution

by:
nishikanth earned 1000 total points
ID: 12471104
to retain your viewstate, add your control on before checking for your post back.


 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim txt As New TextBox()
        txt.ID = "MyTextBoxID"
        plchldr.Controls.Add(txt)             ''''' Add your control here instead of after the end if...

        If Not IsPostBack Then
            txt.Text = "Hello"
            txt.BackColor = Color.Yellow
        End If
end sub

check this link for detail discussion on the above issue....
http://www.experts-exchange.com/Web/Web_Languages/Q_20809587.html#9909168

-----------------------------------------
Load the controls on every post back


   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If viewstate("Add") = "True" Then            ''''Check if the Link Buttons have already been created
            Adddata()
        End If
    End Sub


Sub Adddata()
        'Get a list of the categories
        '2. Create the command object, passing in the SQL string
        Dim strSQL As String
        OleDbConnection1.Open()
        strSQL = "SELECT catno,catname from category"
        Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, OleDbConnection1)

        'Set the datagrid's datasource to the datareader and databind
        Dim myDataReader As OleDbDataReader = myCommand.ExecuteReader()

        'Loop through the results, creating a LinkButton for each row
        Dim filterButton As LinkButton
        While myDataReader.Read()
            'Add the LinkButton

            filterButton = New LinkButton()
            filterButton.ID = myDataReader("CatNo")
            filterButton.Text = myDataReader("CatName")
            filterButton.CommandArgument = myDataReader("catno")
            AddHandler filterButton.Command, AddressOf Me.FilterData

            PlaceHolder1.Controls.Add(filterButton)

        End While

        myDataReader.Close()
        OleDbConnection1.Close()

       viewstate("Add") = True
    End Sub


    Sub FilterData(ByVal sender As Object, ByVal e As CommandEventArgs)
       '''Code for handling event
    End Sub


Also check this for detailed info....
everyone faces this problem with Dynamic controls

http://devcenter.infragistics.com/Articles/ArticleTemplate.Aspx?ArticleID=2149
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
http://www.c-sharpcorner.com/Code/2003/July/DynamicallyCreateASPNETControls.asp
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question