Dynamic rows & controls in a table - problem

Reply

Join Date: Jul 2006
Posts: 6
Reputation: JohnS is an unknown quantity at this point 
Solved Threads: 0
JohnS JohnS is offline Offline
Newbie Poster

Dynamic rows & controls in a table - problem

 
0
  #1
Apr 28th, 2008
Hi!

I'm trying to create a page with dynamic controls, and I'm having difficulty wrt the liffe cycle.

The page has some static controls, but also a table which has dynamic rows, cells of which contain dynamic controls, including a DropDownList.

The idea is that the page has an "Add" button, which should result in a new row being added to the table. The selected values in the earlier DropDownLists must be preserved.


So, first time the page looks like:

[Static Controls]
[TableRow1 [DropDownList1]]
[Add Button]


Clicking the button should result in:

[Static Controls]
[TableRow1 [DropDownList1]]
[TableRow2 [DropDownList2]]
[Add Button]

with DropDownList1 selection being preserved.



The Button click event code tries to read the current selections of the DDLs and store them in session, It then causes a redirect to the same page, forcing the page to be drawn as new (from the life cycle point of view).


My sequence of code is:

  1. protected void Page_Load(object sender, EventArgs e) {
  2. if (!IsPostBack) {
  3. populate();
  4. }
  5. populateVolatile();
  6. }

where populate() sets up the static controls with data retrieved from a database (first time) or session (subsequently), and

populateVolatile() creates and fills the dynamic controls depending on the session data, including DDL selections.

It contains the code:

  1. // add each type/num info to table
  2. foreach (TypeNumInfo tni in volInfo.typeNumRows) {
  3. TableRow tr = new TableRow();
  4.  
  5. // index number
  6. TableCell tc1 = new TableCell();
  7. tc1.Text = tni.idx.ToString();
  8. tr.Cells.Add(tc1);
  9.  
  10. // add a DDL
  11. TableCell tc2 = new TableCell();
  12. DropDownList ddl = new DropDownList();
  13.  
  14. // add ID and list data
  15. ddl.ID = "ddl" + tni.idx;
  16. foreach (ListItem li in dTypes) {
  17. ddl.Items.Add(li);
  18. }
  19.  
  20. // set the selected
  21. // TODO not working
  22. ddl.SelectedIndex = tni.typeSel;
  23.  
  24. tc2.Controls.Add(ddl);
  25. tr.Cells.Add(tc2);
  26.  
  27. tblTypeNum.Rows.Add(tr);
  28. }

The button click code is as follows:

  1. protected void bAddTypeNum_Click(object sender, EventArgs e) {
  2. // get dept, applies selections from static controls
  3. statInfo.selDept = ddlDept.SelectedIndex;
  4. statInfo.selApplies = ddlApplies.SelectedIndex;
  5.  
  6. // create new type/num info representing tablerow data
  7. TypeNumInfo tni = new TypeNumInfo();
  8. int lastIdx = volInfo.typeNumRows.Count;
  9. tni.idx = lastIdx + 1;
  10.  
  11. // add to list
  12. volInfo.typeNumRows.Add(tni);
  13.  
  14. // cache previous rows
  15. int i = 0;
  16. foreach (TableRow tr in tblTypeNum.Rows) {
  17. // if row has droplist, i.e. more than 1 cell
  18. if (tr.Cells.Count > 1) {
  19. DropDownList ddl = (DropDownList)tr.Cells[1].Controls[0];
  20.  
  21. // get the selection in that row
  22. // TODO not working reliably
  23. volInfo.typeNumRows[i++].typeSel = ddl.SelectedIndex;
  24. }
  25. }
  26.  
  27. // update session
  28. Session[strStat] = statInfo;
  29. Session[strVol] = volInfo;
  30.  
  31. // draw as new page
  32. Response.Redirect("");
  33. }

I know the redirect creates an extra trip to the server, but I couldn't find a useful way to enable redrawing after the button event.

Everything works fine except for the DDL selection handling - I cannot set or correctly read the selections.

Help!


John
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC