Last post Jun 14, 2011 10:55 PM by Zizhuoye Chen - MSFT
Jun 10, 2011 03:07 PM|PierPosthumus|LINK
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:
connection = new
command = connection.CreateCommand();
QUERY GOES HERE";
reader = command.ExecuteReader();
itemnumber = new
unitprice = new
cost = new
itemname = new
itemnumber.Text = reader["itemnumber"].ToString();
itemname.Text = reader["itemname"].ToString();
unitprice.Text = reader["baseprice"].ToString();
cost.Text = reader["cost"].ToString();
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?
Jun 10, 2011 03:48 PM|_Manvel_|LINK
For dynamically added controls you should rebind them to datasource on each postback.
Jun 14, 2011 10:55 PM|Zizhuoye Chen - MSFT|LINK
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:
Hope this can help you.