Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42231

VS 2010 Issue with accessing data from dynamically created DropdownLists

$
0
0
I have it setup so that when a user selects an item from a DropdownList, that some DropdownLists are dynamically created, based on the results from the Database that are returned after selecting the item in the first DropDownList.

Then, I have a button that takes the data and saves it to an object I've created for this, and I try to iterate through the Controls of the Placeholder that I added the DropdownList controls to.

However, the Placeholder says there are no controls in it, when I press the button, but I can obviously see them there.

I'm a little confused as to why this happening. I've seen some tutorials but they don't seem to be helping at all. Here is some code:


When a "Pilot" is selected, I get all of the Upgrade Types that that "Pilot" has access to, then I create a DropdownList for each type of Upgrade with all the actual Upgrades of that Type:
VB Code:
  1. For Each u As UpgradeTypeList In upgrades
  2.             Dim lbl As New Label
  3.             Dim ddl As New DropDownList
  4.  
  5.             ddl.AutoPostBack = False
  6.  
  7.             Dim TR_Row As New TableRow
  8.             Dim TD_Label, TD_Dropdown As New TableCell
  9.  
  10.             TD_Label.Attributes.Add("style", "padding: 2px;")
  11.             TD_Dropdown.Attributes.Add("style", "padding: 2px;")
  12.  
  13.             TD_Label.Attributes.Add("align", "right")
  14.  
  15.             lbl.Text = String.Format("{0}: ", u.Type)
  16.  
  17.             Dim ucList As List(Of UpgradeDropdown) = (From x In db.tblCards_Upgrades
  18.                                                       Where x.Type = u.ID
  19.                                                       Select New UpgradeDropdown With {
  20.                                                           .Name = String.Format("{0} ({1})", x.Title, x.PointCost),
  21.                                                           .UpgradeID = x.UpgradeID
  22.                                                       }).ToList
  23.  
  24.             ddl.Items.Add(New ListItem("", 0))
  25.             For Each x As UpgradeDropdown In ucList
  26.                 ddl.Items.Add(New ListItem(x.Name, x.UpgradeID))
  27.             Next
  28.  
  29.             TD_Label.Controls.Add(lbl)
  30.             TD_Dropdown.Controls.Add(ddl)
  31.  
  32.             TR_Row.Controls.Add(TD_Label)
  33.             TR_Row.Controls.Add(TD_Dropdown)
  34.  
  35.             rowUpgrades.Controls.Add(TR_Row)
  36.         Next

Then, when the "Add Squad Member" button is pressed, I am trying to iterate through the controls, pick out any DropdownLists and get the values from them and add them to a List Of in an object for the Pilot info:
VB Code:
  1. Protected Sub btnAddSquadMember_Click(sender As Object, e As EventArgs) Handles btnAddSquadMember.Click
  2.         For Each c As Control In rowUpgrades.Controls
  3.             If TypeOf c Is DropDownList Then
  4.                 Dim DoesExist As Boolean = False
  5.  
  6.                 For Each x As SquadMember_Upgrade In Global_asax.TempPilot.Upgrades
  7.  
  8.                     If x.ID = sender.ID Then
  9.                         DoesExist = True
  10.                     End If
  11.  
  12.                     If DoesExist Then
  13.                         x.Name = sender.SelectedValue
  14.                         Exit Sub
  15.                     End If
  16.                 Next
  17.  
  18.                 Dim newUpgrade As New SquadMember_Upgrade
  19.                 newUpgrade.ID = sender.ID
  20.                 newUpgrade.Name = sender.SelectedValue
  21.  
  22.                 Global_asax.TempPilot.Upgrades.Add(newUpgrade)
  23.             End If
  24.         Next
  25.  
  26.         Global_asax.SquadMembers.Add(Global_asax.TempPilot)
  27.     End Sub

But, the controls supposedly don't exist and no values get added to the List(Of).

Viewing all articles
Browse latest Browse all 42231

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>