thanks jmurray, I got this code below that worked. It somehow captures the dropdownlist values. I added the database code or insert statement this morning and it stores the dropdown values in sql. I'm going to use this, but as always dude, thanks for your help.
code i got:
Code:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Me.createDDLs()
End Sub
Public Sub MyDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = TryCast(sender, DropDownList)
'Response.Write(ddl.SelectedItem.Text.ToString())
ddl.DataBind()
End Sub
Public Sub createDDLs()
Dim num As Integer = 0
If Not IsNothing(Request.Item("txtNumber")) Then
num = CType(Request.Item("txtNumber"), Integer)
End If
Me.Panel1.Controls.Clear()
Dim I As Integer
For I = 1 To num
Dim MyDDL As New DropDownList
MyDDL.ID = "ddlDynamic" & I
AddHandler MyDDL.SelectedIndexChanged, AddressOf MyDDL_SelectedIndexChanged
'MyDDL.AutoPostBack = True
Panel1.Controls.Add(MyDDL)
MyDDL.DataSourceID = SqlDataSource1.ID
MyDDL.DataValueField = "agency_name"
MyDDL.DataBind()
Dim MyLiteral = New LiteralControl
MyLiteral.Text = "<BR><BR>"
Panel1.Controls.Add(MyLiteral)
Next
IterateThroughChildren(Panel1)
'Dim dynDDL As String() = Session("controls").ToString.Split(",")
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Trim(txtNumber.Text) <> "0" And Not String.IsNullOrEmpty(txtNumber.Text) Then
Dim i As Integer
Dim numOfDDL As Integer
numOfDDL = txtNumber.Text
Dim ddlSelected As String
For i = 1 To numOfDDL
ddlSelected = Request.Item("ddlDynamic" & i).ToString
'Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic" & i), DropDownList)
'dropdownlist1.Items.FindByText(ddlSelected).Selected = True
Dim dataadapter As SqlDataAdapter
Dim datacommand As SqlCommand
dataadapter = New SqlDataAdapter
datacommand = New SqlCommand
datacommand.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings("peebman").ToString)
datacommand.CommandText = "Insert into dbo.title (testname, testtile) values('" & txtNumber.Text & "','" & ddlSelected & "')"
'Response.Write(Split(ddlSelected))
'Response.Write("Insert Into Table(column) values('" & ddlSelected & "')")
datacommand.Connection.Open()
Try
datacommand.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message.ToString() + " not working")
Response.End()
Finally
datacommand.Connection.Close()
End Try
Next
End If
End Sub
Private Sub IterateThroughChildren(ByVal parent As Control)
For Each c As Control In parent.Controls
' ...do something...
'If c.ID.Substring(0, 2) = "gv" Then
'Response.Write(c.ID.ToString.Substring(0, 1))
If c.GetType.ToString.Equals("System.Web.UI.WebControls.DropDownList") Then
Session("controls") += c.ID + ","
End If
'End If
If c.[GetType]().ToString().Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then
End If
If c.Controls.Count > 0 Then
IterateThroughChildren(c)
End If
Next
End Sub
Protected Sub txtNumber_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumber.TextChanged
btnSave.Visible = True
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtNumber.Focus()
End Sub

Originally Posted by
jmurrayhead
Try this, instead:
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To txtNumber.Text
Dim dropdownlist1 As DropDownList = CType(form1.FindControl("ddldynamic" & i), DropDownList)
If DropDownList1 Is Nothing Then
Response.Write("Dropdown was not found<br />")
Else
Response.Write(dropdownlist1.SelectedValue)
End If
'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
Next
End Sub
Bookmarks