locked
Viewstate and Dynamically added controls RRS feed

  • Question

  • User2137039197 posted

    Hi All, first time poster here, so please go easy on me :)

    I am trying to build a tool for our salespeople to use where they can add a variable number of items to a quote for a customer. The asp.net web form consists of a few textboxes for basic customer info, and a placeholder (itemList) that I would like to fill with a row of textboxes each time the salesperson clicks on "Add new item". The basic function I have to query the DB and add the row works just fine, but the problem is that each time "Add new item" is clicked, the existing textboxes I have are replaced with the new data - I can't seem to find a way to just "Add" to the list and the viewstate does not remember what was added.

     

    Here is the code for the "Add new item" click event:

     

    protected void lookupItem(object sender, EventArgs e)

        {

           

    SqlConnection connection = new SqlConnection(Session["dbconn"].ToString());

            connection.Open();

     

          SqlCommand command = connection.CreateCommand();

            command.CommandText ="SQL QUERY GOES HERE";

           SqlDataReader reader = command.ExecuteReader();

           while (reader.Read())

            {

               TextBox itemnumber = new TextBox();

               TextBox unitprice = new TextBox();

               TextBox cost = new TextBox();

             TextBox itemname = new TextBox();

                itemnumber.Text = reader["itemnumber"].ToString();

                itemname.Text = reader["itemname"].ToString();

                unitprice.Text = reader["baseprice"].ToString();

                cost.Text = reader["cost"].ToString();

               

                itemList.Controls.Add( itemnumber);

                itemList.Controls.Add(itemname);

                itemList.Controls.Add( unitprice);

                itemList.Controls.Add( cost);

               

            }

            reader.Close();

            connection.Close();

        }

    Is there something special that I have to do to save the viewstate after this function is called, or am I just approaching this in a completely wrong fashion?

    Friday, June 10, 2011 3:07 PM

Answers

  • User-29804325 posted

    Hi,

    With your code, I assume that you re-create the control and retrieve the data from database. Yes, it's right. But I'm not sure how do you load this control in postback. If possible, can you please post more relevant code here and let us get more details.

    To implement your requirement. I think you can assign a viewstate, on first load, query DB and get the sales information, create the row and control based on these information as your've done in your code. At the same time, create a object like DataTable, DataSet or List collection that contain these sales data, set it in ViewState. Then on each postback, check ViewState and re-create controls base on ViewState data.

    Alternative way, you can consider to use some data holder control like ListView control. It's easy to use these control to bind data. And also ListView has built-in add new item feature. You can check these samples:

    http://aspalliance.com/1542_Data_Manipulation_using_ListView_Server_Control_with_ASPNET_35.4

    http://www.codeproject.com/KB/webforms/CompleteListView.aspx

    Hope this can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2011 10:55 PM

All replies

  • User-1423995609 posted

    For dynamically added controls you should rebind them to datasource on each postback.

    Friday, June 10, 2011 3:48 PM
  • User-29804325 posted

    Hi,

    With your code, I assume that you re-create the control and retrieve the data from database. Yes, it's right. But I'm not sure how do you load this control in postback. If possible, can you please post more relevant code here and let us get more details.

    To implement your requirement. I think you can assign a viewstate, on first load, query DB and get the sales information, create the row and control based on these information as your've done in your code. At the same time, create a object like DataTable, DataSet or List collection that contain these sales data, set it in ViewState. Then on each postback, check ViewState and re-create controls base on ViewState data.

    Alternative way, you can consider to use some data holder control like ListView control. It's easy to use these control to bind data. And also ListView has built-in add new item feature. You can check these samples:

    http://aspalliance.com/1542_Data_Manipulation_using_ListView_Server_Control_with_ASPNET_35.4

    http://www.codeproject.com/KB/webforms/CompleteListView.aspx

    Hope this can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2011 10:55 PM