I use 2 dropdownlist which is parent-child controls that show list of data from SQL Server. I wanted to make it happen when I selected second dropdownlist, the DetailsView will display result from what I selected. If I select database name is Database3, I want DetailsView to show all result from database3. I tried do some coding and the detailsview doesn’t show anything. Please help! Here my dropdownlist codes,
<asp:DropDownList ID="DropDownServerName" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="ServerName" DataValueField="ServerName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]"></asp:SqlDataSource>
<br />
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName" OnSelectedIndexChanged="DropDownDatabase_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownServerName" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Then in DetailsView, I don’t want to put too much codes for you have to go look all trough but the DetailsView had Update, Delete, and Insert Function. They come with DataSourceID is SQLDataSource1 which it BoundField from table Database. In SQLDataSource1, on SelectCommand, I type,
SelectCommand="SELECT * FROM [tblDatabase] WHERE [DatabaseName] = @DatabaseName"
Then I add SelectParameter in SQLDataSource1 from selected dropdownlist for this,
<SelectParameters>
<asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" />
</SelectParameters
Here is some code for a page I built using the same requirements. I had two drop down lists, one for names of customers, and the other for the products they bought. The detailsview would display the full details for any product selected in the second drop drop list.
I think this is what you were going for. Keep in mind that when you set up the datasources for the 2nd drop down list and the detailsview you have to make sure that you use the wizard to create a where clause so that both grab the primary keys from the drop down list controls.
For example the sqldatasource1 (see bottom of code) has the ControlID set as “ddlProducts” with the PropertyName as “SelectedValue”. The detailsview using the sqlDataSource1 runs the select statement using the selected value of the Products dropdownlist. Hopefully this helps.