Multiple DDLs populating different fields in a form

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Multiple DDLs populating different fields in a form

 
0
  #1
Feb 14th, 2008
I have two drop downs (the 1st pulls from 1 db table then populates the second which pulls from a 2nd db table) with the idea of having them both populate fields of a form on autopostback. The user will then fill out the remaining empty fields.

I have not been able to get anywhere with populating the form text boxes as I'm not yet too proficient asp.net/vb realm.

Any help is greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: Multiple DDLs populating different fields in a form

 
0
  #2
Feb 14th, 2008
If I understood your question correctly, the following steps should give you what you want.

1) Set AutoPostBack=True for the dropdownlists
2) Create an event that sets the form field's value to the selected value or text of the dropdownlist.

Here's a quick example.

ASPX
  1. <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true">
  2. <asp:ListItem Text="Select" Value=""></asp:ListItem>
  3. <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
  4. </asp:DropDownList>
  5. <asp:TextBox ID="txt" runat="server">

Code-Behind
  1. Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
  2. txt.Text = ddl.SelectedValue
  3. End Sub

I'm assuming that instead of just assigning the dropdownlist's SelectedValue to the textbox's text property, you'll use the SelectedValue to calculate a new value or run a through a stored procedure to get a new value, but I may be wrong.

Good luck!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Multiple DDLs populating different fields in a form

 
0
  #3
Feb 14th, 2008
Thanks for the response. I've attached a visual of what I'm trying to do. Some of the info posted in the text boxes will be pulled from the correct tables in database and some will be exactly what was selected in the drop downs. Here is my current code.

aspx

  1.  
  2. <span class="style5">Select a Company:</span>
  3. <asp:DropDownList
  4. ID="DropDownList1"
  5. runat="server"
  6. DataSourceID="SqlDataSource1"
  7. DataTextField="Company"
  8. DataValueField="cusID"
  9. CssClass="maintext"
  10. ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True">
  11. <asp:ListItem Value="">Select One</asp:ListItem>
  12. </asp:DropDownList>
  13. </td>
  14. </tr>
  15. <tr>
  16. <td
  17. <span class="maintext"><b>Select a Contact:</b> &nbsp;</span>&nbsp;
  18. <asp:DropDownList
  19. ID="DropDownList2"
  20. runat="server"
  21. DataSourceID="SqlDataSource2"
  22. DataTextField="contID"
  23. DataValueField="contactID"
  24. CssClass="maintext"
  25. ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True">
  26. <asp:ListItem Value="">Select One</asp:ListItem>
  27. </asp:DropDownList>
  28. <asp:Label ID="labelSelection"
  29. runat="server" style="z-index: 102; left: 368px; position: absolute; top: 144px">
  30. </asp:Label>
  31. </td>
  32. </tr>
  33. <tr>
  34. <td class="style6">
  35. <asp:HyperLink
  36. ID="AddCompany"
  37. runat="server"
  38. CssClass="maintext"
  39. Font-Bold="True"
  40. ForeColor="Maroon"
  41. NavigateUrl="~/ServiceExpress/DisAddCo.aspx">Add New Company?</asp:HyperLink>
  42. <br />
  43. <br />
  44. <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
  45. <InsertItemTemplate>
  46. <table cellpadding="0"
  47. class="style2">
  48. <tr>
  49. <td class="maintext">
  50. Company:</td>
  51. <td colspan="5" style="margin-left: 40px">
  52. <asp:TextBox ID="Company" runat="server" Width="250px" ReadOnly="True"></asp:TextBox>
  53. </td>
  54. <td class="maintext">
  55. Phone:</td>
  56. <td>
  57. <asp:TextBox ID="Phone" runat="server" ReadOnly="True"></asp:TextBox>
  58. </td>
  59. </tr>
  60. <tr>
  61. <td class="maintext">
  62. First Name:</td>
  63. <td colspan="5">
  64. <asp:TextBox ID="FirstName" runat="server" Width="200px" ReadOnly="True"></asp:TextBox>
  65. </td>
  66. <td class="style10">
  67. &nbsp;</td>
  68. <td>
  69. &nbsp;</td>
  70. </tr>
  71. <tr>
  72. <td class="maintext">
  73. Last Name:
  74. </td>
  75. <td colspan="5">
  76. <asp:TextBox ID="LastName" runat="server" style="margin-bottom: 0px"
  77. Width="200px" ReadOnly="True"></asp:TextBox>
  78. </td>
  79. <td class="maintext">
  80. Technician:</td>
  81. <td>
  82. <asp:DropDownList ID="DropDownListTech" runat="server" CssClass="maintext"
  83. DataSourceID="SqlDataSource3" DataTextField="techid"
  84. DataValueField="UserID" AppendDataBoundItems="True">
  85. <asp:ListItem Value="">Unassigned</asp:ListItem>
  86. </asp:DropDownList>
  87. </td>
  88. </tr>
  89. <tr>
  90. <td class="maintext">
  91. Address:</td>
  92. <td colspan="5">
  93. <asp:TextBox ID="Address1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox>
  94. </td>
  95. <td class="maintext">
  96. Priority:</td>
  97. <td>
  98. <asp:DropDownList ID="DropDownListPriority" runat="server" CssClass="maintext">
  99. <asp:ListItem Selected="True">Select One</asp:ListItem>
  100. <asp:ListItem>Red (High)</asp:ListItem>
  101. <asp:ListItem>Yellow</asp:ListItem>
  102. <asp:ListItem>Blue</asp:ListItem>
  103. <asp:ListItem>Green (Low)</asp:ListItem>
  104. </asp:DropDownList>
  105. </td>
  106. </tr>
  107. <tr>
  108. <td class="style15">
  109. &nbsp;</td>
  110. <td colspan="5">
  111. <asp:TextBox ID="Address2" runat="server" Width="200px"
  112. style="margin-bottom: 0px" ReadOnly="True"></asp:TextBox>
  113. </td>
  114. <td class="maintext">
  115. Billing:</td>
  116. <td>
  117. <asp:DropDownList ID="DropDownListBill" runat="server" CssClass="maintext">
  118. <asp:ListItem Selected="True">Select One</asp:ListItem>
  119. <asp:ListItem>Contract</asp:ListItem>
  120. <asp:ListItem>Time/Materials</asp:ListItem>
  121. </asp:DropDownList>
  122. </td>
  123. </tr>
  124. <tr>
  125. <td class="maintext">
  126. City:</td>
  127. <td class="style14">
  128. <asp:TextBox ID="City" runat="server" ReadOnly="True"></asp:TextBox>
  129. </td>
  130. <td class="maintext">
  131. State:</td>
  132. <td class="style12">
  133. <asp:TextBox ID="State" runat="server" Width="30px" ReadOnly="True"></asp:TextBox>
  134. </td>
  135. <td class="maintext">
  136. Zip:</td>
  137. <td class="style11">
  138. <asp:TextBox ID="Zip" runat="server" Width="80px" ReadOnly="True"></asp:TextBox>
  139. </td>
  140. <td class="style10">
  141. &nbsp;</td>
  142. <td>
  143. &nbsp;</td>
  144. </tr>
  145. <tr>
  146. <td class="style15">
  147. &nbsp;</td>
  148. <td class="style14">
  149. &nbsp;</td>
  150. <td class="style13">
  151. &nbsp;</td>
  152. <td class="style12">
  153. &nbsp;</td>
  154. <td class="style9">
  155. &nbsp;</td>
  156. <td class="style11">
  157. &nbsp;</td>
  158. <td class="style10">
  159. &nbsp;</td>
  160. <td>
  161. &nbsp;</td>
  162. </tr>
  163. <tr>
  164. <td class="maintext">
  165. Equipment:</td>
  166. <td class="style14">
  167. <asp:DropDownList ID="DropDownListEquip" runat="server" CssClass="maintext">
  168. </asp:DropDownList>
  169. </td>
  170. <td class="style13">
  171. &nbsp;</td>
  172. <td class="style12">
  173. &nbsp;</td>
  174. <td class="style9">
  175. &nbsp;</td>
  176. <td class="style11">
  177. &nbsp;</td>
  178. <td class="style10">
  179. &nbsp;</td>
  180. <td>
  181. &nbsp;</td>
  182. </tr>
  183. <tr>
  184. <td class="maintext" valign="top">
  185. Problem:</td>
  186. <td class="style8" colspan="5">
  187. <asp:TextBox ID="Problem" runat="server" Height="100px" TextMode="MultiLine"
  188. Width="400px"></asp:TextBox>
  189. </td>
  190. <td class="maintext" valign="top">
  191. Notes:</td>
  192. <td>
  193. <asp:TextBox ID="Notes" runat="server" Height="100px" TextMode="MultiLine"
  194. Width="400px"></asp:TextBox>
  195. </td>
  196. </tr>
  197. <tr>
  198. <td class="style7" colspan="8" valign="top">
  199. <asp:Button ID="Button1" runat="server" Text="Submit" />
  200. &nbsp;<asp:Button ID="Button2" runat="server" Text="Reset" />
  201. </td>
  202. </tr>
  203. </table>
  204. </InsertItemTemplate>
  205. </asp:FormView>

Code behind

  1.  
  2.  
  3. Partial Class AddCall
  4. Inherits System.Web.UI.Page
  5. Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  6. labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text
  7.  
  8. End Sub
  9.  
  10. End Class
Last edited by foundsheep; Feb 14th, 2008 at 1:43 pm.
Attached Thumbnails
dropdown.gif  
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: Multiple DDLs populating different fields in a form

 
0
  #4
Feb 14th, 2008
Oh I see. So what is it that you're currently unsure of? How to query your database with the selected value from the drop down list? Just let us know.

Jake
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Multiple DDLs populating different fields in a form

 
0
  #5
Feb 14th, 2008
Yes, I am not sure how that would be done with two drop downs from two different tables. I've found plenty about one drop down populating something, but not this.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: Multiple DDLs populating different fields in a form

 
0
  #6
Feb 14th, 2008
Sure. Here's some code to get you started. You'll want to do this in the SelectedIndexChanged event handler for both dropdowns (changing the appropriate parts of the code). You'll need to change anything prefixed with "your_" to match your settings.

  1.  
  2. Dim str_sql As String = "SELECT * FROM your_table WHERE cusID = " & DropDownList1.SelectedValue
  3. Using connection As New System.Data.SqlClient.SqlConnection("your_connection_string")
  4. Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection)
  5. connection.Open()
  6. Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader()
  7. Try
  8. If reader.Read Then
  9. Address1.Text = reader.Item("your_column_name").ToString
  10. End If
  11. Finally
  12. ' Always call Close when done reading.
  13. reader.Close()
  14. End Try
  15. End Using
  16.  
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Multiple DDLs populating different fields in a form

 
0
  #7
Feb 14th, 2008
Ok, I'm not exactly sure what you mean by "do this in the SelectedIndexChanged event hander."

Nothing happens with this in my code behind:

  1.  
  2.  
  3. Partial Class AddCall
  4. Inherits System.Web.UI.Page
  5. Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  6. labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text
  7.  
  8. Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
  9.  
  10. Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue
  11. Using connection As New System.Data.SqlClient.SqlConnection("HRIServiceConnectionString1")
  12. Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection)
  13. connection.Open()
  14. Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader()
  15. Try
  16. If reader.Read Then
  17. Address1.Text = reader.Item("Address1").ToString
  18. End If
  19. Finally
  20. ' Always call Close when done reading.
  21. reader.Close()
  22. End Try
  23. End Using
  24.  
  25. End Sub
  26.  
  27. End Class
  28.  
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: Multiple DDLs populating different fields in a form

 
0
  #8
Feb 14th, 2008
Try changing
  1. DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

to
  1. DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Multiple DDLs populating different fields in a form

 
0
  #9
Feb 14th, 2008
Holy crap its almost working!!!! Its posting the name and contact randomly above the drop downs as well as in the text boxes. How do I get rid of the the ones above?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: Multiple DDLs populating different fields in a form

 
0
  #10
Feb 14th, 2008
Lol. I'm glad you're getting close!

I'm not 100% sure what you mean, but you may want to try removing this line.

  1. labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC