| | |
Multiple DDLs populating different fields in a form
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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.
•
•
Join Date: Feb 2008
Posts: 62
Reputation:
Solved Threads: 4
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
Code-Behind
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!
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
ASP.NET Syntax (Toggle Plain Text)
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"> <asp:ListItem Text="Select" Value=""></asp:ListItem> <asp:ListItem Text="Option 1" Value="1"></asp:ListItem> </asp:DropDownList> <asp:TextBox ID="txt" runat="server">
Code-Behind
ASP.NET Syntax (Toggle Plain Text)
Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged txt.Text = ddl.SelectedValue 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!
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
Code behind
aspx
ASP.NET Syntax (Toggle Plain Text)
<span class="style5">Select a Company:</span> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Company" DataValueField="cusID" CssClass="maintext" ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True"> <asp:ListItem Value="">Select One</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td <span class="maintext"><b>Select a Contact:</b> </span> <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="contID" DataValueField="contactID" CssClass="maintext" ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True"> <asp:ListItem Value="">Select One</asp:ListItem> </asp:DropDownList> <asp:Label ID="labelSelection" runat="server" style="z-index: 102; left: 368px; position: absolute; top: 144px"> </asp:Label> </td> </tr> <tr> <td class="style6"> <asp:HyperLink ID="AddCompany" runat="server" CssClass="maintext" Font-Bold="True" ForeColor="Maroon" NavigateUrl="~/ServiceExpress/DisAddCo.aspx">Add New Company?</asp:HyperLink> <br /> <br /> <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert"> <InsertItemTemplate> <table cellpadding="0" class="style2"> <tr> <td class="maintext"> Company:</td> <td colspan="5" style="margin-left: 40px"> <asp:TextBox ID="Company" runat="server" Width="250px" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> Phone:</td> <td> <asp:TextBox ID="Phone" runat="server" ReadOnly="True"></asp:TextBox> </td> </tr> <tr> <td class="maintext"> First Name:</td> <td colspan="5"> <asp:TextBox ID="FirstName" runat="server" Width="200px" ReadOnly="True"></asp:TextBox> </td> <td class="style10"> </td> <td> </td> </tr> <tr> <td class="maintext"> Last Name: </td> <td colspan="5"> <asp:TextBox ID="LastName" runat="server" style="margin-bottom: 0px" Width="200px" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> Technician:</td> <td> <asp:DropDownList ID="DropDownListTech" runat="server" CssClass="maintext" DataSourceID="SqlDataSource3" DataTextField="techid" DataValueField="UserID" AppendDataBoundItems="True"> <asp:ListItem Value="">Unassigned</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td class="maintext"> Address:</td> <td colspan="5"> <asp:TextBox ID="Address1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> Priority:</td> <td> <asp:DropDownList ID="DropDownListPriority" runat="server" CssClass="maintext"> <asp:ListItem Selected="True">Select One</asp:ListItem> <asp:ListItem>Red (High)</asp:ListItem> <asp:ListItem>Yellow</asp:ListItem> <asp:ListItem>Blue</asp:ListItem> <asp:ListItem>Green (Low)</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td class="style15"> </td> <td colspan="5"> <asp:TextBox ID="Address2" runat="server" Width="200px" style="margin-bottom: 0px" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> Billing:</td> <td> <asp:DropDownList ID="DropDownListBill" runat="server" CssClass="maintext"> <asp:ListItem Selected="True">Select One</asp:ListItem> <asp:ListItem>Contract</asp:ListItem> <asp:ListItem>Time/Materials</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td class="maintext"> City:</td> <td class="style14"> <asp:TextBox ID="City" runat="server" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> State:</td> <td class="style12"> <asp:TextBox ID="State" runat="server" Width="30px" ReadOnly="True"></asp:TextBox> </td> <td class="maintext"> Zip:</td> <td class="style11"> <asp:TextBox ID="Zip" runat="server" Width="80px" ReadOnly="True"></asp:TextBox> </td> <td class="style10"> </td> <td> </td> </tr> <tr> <td class="style15"> </td> <td class="style14"> </td> <td class="style13"> </td> <td class="style12"> </td> <td class="style9"> </td> <td class="style11"> </td> <td class="style10"> </td> <td> </td> </tr> <tr> <td class="maintext"> Equipment:</td> <td class="style14"> <asp:DropDownList ID="DropDownListEquip" runat="server" CssClass="maintext"> </asp:DropDownList> </td> <td class="style13"> </td> <td class="style12"> </td> <td class="style9"> </td> <td class="style11"> </td> <td class="style10"> </td> <td> </td> </tr> <tr> <td class="maintext" valign="top"> Problem:</td> <td class="style8" colspan="5"> <asp:TextBox ID="Problem" runat="server" Height="100px" TextMode="MultiLine" Width="400px"></asp:TextBox> </td> <td class="maintext" valign="top"> Notes:</td> <td> <asp:TextBox ID="Notes" runat="server" Height="100px" TextMode="MultiLine" Width="400px"></asp:TextBox> </td> </tr> <tr> <td class="style7" colspan="8" valign="top"> <asp:Button ID="Button1" runat="server" Text="Submit" /> <asp:Button ID="Button2" runat="server" Text="Reset" /> </td> </tr> </table> </InsertItemTemplate> </asp:FormView>
Code behind
ASP.NET Syntax (Toggle Plain Text)
Partial Class AddCall Inherits System.Web.UI.Page Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text End Sub End Class
Last edited by foundsheep; Feb 14th, 2008 at 1:43 pm.
•
•
Join Date: Feb 2008
Posts: 62
Reputation:
Solved Threads: 4
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.
ASP.NET Syntax (Toggle Plain Text)
Dim str_sql As String = "SELECT * FROM your_table WHERE cusID = " & DropDownList1.SelectedValue Using connection As New System.Data.SqlClient.SqlConnection("your_connection_string") Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection) connection.Open() Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader() Try If reader.Read Then Address1.Text = reader.Item("your_column_name").ToString End If Finally ' Always call Close when done reading. reader.Close() End Try End Using
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:
Nothing happens with this in my code behind:
ASP.NET Syntax (Toggle Plain Text)
Partial Class AddCall Inherits System.Web.UI.Page Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox) Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue Using connection As New System.Data.SqlClient.SqlConnection("HRIServiceConnectionString1") Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection) connection.Open() Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader() Try If reader.Read Then Address1.Text = reader.Item("Address1").ToString End If Finally ' Always call Close when done reading. reader.Close() End Try End Using End Sub End Class
•
•
Join Date: Feb 2008
Posts: 62
Reputation:
Solved Threads: 4
Try changing
to
ASP.NET Syntax (Toggle Plain Text)
DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
to
ASP.NET Syntax (Toggle Plain Text)
DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
•
•
Join Date: Feb 2008
Posts: 62
Reputation:
Solved Threads: 4
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.
I'm glad you're getting close! I'm not 100% sure what you mean, but you may want to try removing this line.
ASP.NET Syntax (Toggle Plain Text)
labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to import excel file data to mysql database
- Next Thread: Multiple Identity Columns In SQL
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox click commonfunctions compatible confirmationcodegeneration content contenttype courier css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu dynamically edit expose fill flash flv formatdecimal forms formview gridview homeedition iframe iis javascript jquery listbox login menu microsoft mono mouse mssql multistepregistration news numerical objects opera order panelmasterpagebuttoncontrols radio ratings registration reportemail rotatepage save schoolproject search security serializesmo.table silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers





