This is a discussion on dynamically created dropdowns insert to sql within the .Net Development forums, part of the Programming & Scripting category; Hey everyone its peebman2000, currently still building the questionnaire vb.net app for my client. I've never worked with creating dynamic ...
| |||||||
|
#1
| |||
| |||
| Hey everyone its peebman2000, currently still building the questionnaire vb.net app for my client. I've never worked with creating dynamic controls before, but a functionality the client wants, I feel creating dropdownlist dynamically would work. Below is code I found on line and it works, but I need to insert the data or values from each dynamically created dropdownlist into sql. I have no clue on how to do that, I know I'll have to creat another for loop, but i'm lost on how to grab or capture the selected values from the dropdownlist that are created. Does anyone know how to grab the values from dynamically created dropdownlists? So I may be able to store the data in SQL. Thanks for the help. aspx.vb page: Code: Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
'Dim dt As SqlDataSource
'dt = Sqldatasource1
'dt.DataBind()
''code to dynamically create dropdownlist
'Dim I As Integer
'Dim J As Integer
'For I = 1 To txtNumber.Text
' Dim MyDDL = New DropDownList
' MyDDL.ID = "ddlDynamic" & I
' For J = 1 To 3
' Dim MyLI As New ListItem
' 'MyLI.Text = "Control Number: " & I & "-" & J
' 'MyLI.Value = I & J
' MyDDL.Items.Add(MyLI)
' ''MyDDL.Items.Add(dt)
' 'MyDDL.DataSourceID = Sqldatasource1.ID
' 'MyDDL.DataBind()
' Next
' form1.Controls.Add(MyDDL)
' Dim MyLiteral = New LiteralControl
' MyLiteral.Text = "<BR><BR>"
' form1.Controls.Add(MyLiteral)
'Next
'''''code to dynamically create dropdownlist with data bounded to dropdownlist
Dim I As Integer
'Dim J As Integer
For I = 1 To txtNumber.Text
Dim MyDDL As New DropDownList
MyDDL.ID = "ddlDynamic" & I
form1.Controls.Add(MyDDL)
MyDDL.DataSourceID = Sqldatasource1.ID
MyDDL.DataValueField = "agency_name"
MyDDL.DataBind()
Dim MyLiteral = New LiteralControl
MyLiteral.Text = "<BR><BR>"
form1.Controls.Add(MyLiteral)
Next
End Sub
Code: <form id="form1" runat="server">
<div>
<B>Enter the number of DropDownList controls you want:</B><BR><BR>
<asp:TextBox
id="txtNumber"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text="OK"
runat="server"
/>
<BR><BR>
</div>
<asp:sqldatasource ID="Sqldatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:peebman %>" SelectCommand="SELECT agency_name FROM dbo.agency"></asp:sqldatasource>
</form>
|
|
#2
| ||||
| ||||
| Couldn't you just use: ddlDynamic1.SelectedIndex.Value to get the value and then append it to your query?
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
|
#3
| |||
| |||
| Hey thanks, I'll be using a stored procedure to store in sql, but I need to capture the value in the dropdownlists. I tried to do a response.write to see if i'm getting the values from the dropdownlists using ddlDynamic1.selectedindex.value Code: Response.Write(ddlDynamic.selectedindex.value) If I can just capture the values in the dropdownlist, I can insert it into Sql. Any ideas on how I can capture the selected values, because the ddlDynamic.selectedinex.value didn't work or I may be doing something wrong in how I should use your suggestion. Thanks |
|
#5
| |||
| |||
| Thanks, I tried that and it still says: Quote:
Code: Response.Write(ddlDynamic1.selectedindex.value) |
|
#6
| ||||
| ||||
| You'll have to create an instance and use FindControl like so: Code: Dim DropDownList1 As DropDownList = CType(Me.FindControl("ddlDynamic1"), DropDownList)
Response.Write(DropDownList1.SelectedValue)
|
|
#7
| |||
| |||
| Hey Jmurrayhead, that workd, but I need to separate each selectedvalue from each dropdown. i tried using the same for loop like in the code below, but that didn't work. How can I separate each selected value for each dropdownlist? aspx.vb code: Code: Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
'Dim dt As SqlDataSource
'dt = Sqldatasource1
'dt.DataBind()
''code to dynamically create dropdownlist
'Dim I As Integer
'Dim J As Integer
'For I = 1 To txtNumber.Text
' Dim MyDDL = New DropDownList
' MyDDL.ID = "ddlDynamic" & I
' For J = 1 To 3
' Dim MyLI As New ListItem
' 'MyLI.Text = "Control Number: " & I & "-" & J
' 'MyLI.Value = I & J
' MyDDL.Items.Add(MyLI)
' ''MyDDL.Items.Add(dt)
' 'MyDDL.DataSourceID = Sqldatasource1.ID
' 'MyDDL.DataBind()
' Next
' form1.Controls.Add(MyDDL)
' Dim MyLiteral = New LiteralControl
' MyLiteral.Text = "<BR><BR>"
' form1.Controls.Add(MyLiteral)
'Next
'''''code to dynamically create dropdownlist with data bounded to dropdownlist
Dim I As Integer
'Dim J As Integer
For I = 1 To txtNumber.Text
Dim MyDDL As New DropDownList
MyDDL.ID = "ddlDynamic" & I
form1.Controls.Add(MyDDL)
MyDDL.DataSourceID = Sqldatasource1.ID
'MyDDL.AutoPostBack = True
MyDDL.DataValueField = "agency_name"
MyDDL.DataBind()
Dim MyLiteral = New LiteralControl
MyLiteral.Text = "<BR><BR>"
form1.Controls.Add(MyLiteral)
Next
'Dim i As Integer
'For I = 1 To txtNumber.Text
' Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic1"), DropDownList)
' 'dropdownlist1.AutoPostBack = True
' Response.Write(dropdownlist1.SelectedValue)
' 'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
'Next
End Sub
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(Me.FindControl("ddldynamic1"), DropDownList)
Response.Write(dropdownlist1.SelectedValue)
'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
Next
End Sub
|
|
#8
| ||||
| ||||
| Code: For i = 1 To txtNumber.Text
Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic" & i), DropDownList)
Response.Write(dropdownlist1.SelectedValue)
'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
Next
|
|
#9
| |||
| |||
| Kind of, i'm trying to use the button1 click event to display the selectedvalues in each dropdownlist and I need them to be separated. aspx.vb code: 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(Me.FindControl("ddlDynamic" & i), DropDownList)
Response.Write(dropdownlist1.SelectedValue)
'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
Next
End Sub
Quote:
Quote:
|
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| update gridview dynamically | peebman2000 | .Net Development | 27 | May 8th, 2008 10:03 PM |