选择ASP.NET DropDownList使DetailsView显示结果

时间:2012-11-21 14:15:48

标签: asp.net sql-server

我使用2个下拉列表,它是显示来自SQL Server的数据列表的父子控件。当我选择第二个下拉列表时,我想要实现它,DetailsView将显示我选择的结果。如果我选择数据库名称是Database3,我希望DetailsView显示来自database3的所有结果。我尝试做一些编码,详情视图没有显示任何内容。请帮忙!这是我的下拉列表代码,

<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>

然后在DetailsView中,我不想放太多代码,你必须看看所有的低谷,但DetailsView有更新,删除和插入功能。它们带有DataSourceID,是SQLDataSource1,它来自表Database的BoundField。在SQLDataSource1中,在SelectCommand上,我输入

SelectCommand="SELECT * FROM [tblDatabase] WHERE [DatabaseName] = @DatabaseName"

然后我从选定的下拉列表中为SQLDataSource1添加SelectParameter,

<SelectParameters>
            <asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" />
        </SelectParameters

1 个答案:

答案 0 :(得分:0)

以下是我使用相同要求构建的页面的一些代码。我有两个下拉列表,一个用于客户名称,另一个用于他们购买的产品。详细信息视图将显示在第二个下拉列表中选择的任何产品的完整详细信息。

我认为这就是你想要的。请记住,在为第二个下拉列表和detailsview设置数据源时,必须确保使用向导创建where子句,以便从下拉列表控件中获取主键。

例如,sqldatasource1(参见代码底部)将ControlID设置为“ddlProducts”,PropertyName为“SelectedValue”。使用sqlDataSource1的detailsview使用Products下拉列表的选定值运行select语句。希望这会有所帮助。

<table class="style4">
        <tr>
            <td>
                Customer:</td>
            <td>
        <asp:DropDownList ID="ddlCustomers" runat="server" 
            DataSourceID="ObjectDataSource1" AutoPostBack="True" DataTextField="Name" 
            DataValueField="CustomerID" AppendDataBoundItems="True" 
            onselectedindexchanged="ddlCustomers_SelectedIndexChanged">
             <asp:ListItem Text="--Select a customer--" Value="0"></asp:ListItem>
        </asp:DropDownList>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="CustomerDB" SelectMethod="GetCustomerList">
        </asp:ObjectDataSource>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="ddlCustomers" ErrorMessage="Please select a Customer" 
                    InitialValue="0"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Product</td>
            <td>
        <asp:DropDownList ID="ddlProducts" runat="server" AutoPostBack="True" 
            DataSourceID="ObjectDataSource2" DataTextField="Name" 
            DataValueField="ProductCode" AppendDataBoundItems="True" 
                    onselectedindexchanged="ddlProducts_SelectedIndexChanged">
            <asp:ListItem Text="--Select a product--" Value="0"></asp:ListItem>
        </asp:DropDownList>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
            SelectMethod="GetCustomerProducts" TypeName="ProductDB" 
            OldValuesParameterFormatString="original_{0}">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlCustomers" Name="CustomerID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="ddlProducts" ErrorMessage="Please select a product" 
                    InitialValue="0"></asp:RequiredFieldValidator>
            </td>
        </tr>

    </table>


    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="ProductCode" DataSourceID="SqlDataSource1" Height="50px" 
        Width="125px">
        <Fields>
            <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" 
                ReadOnly="True" SortExpression="ProductCode" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Version" HeaderText="Version" 
                SortExpression="Version" />
            <asp:BoundField DataField="ReleaseDate" HeaderText="ReleaseDate" 
                SortExpression="ReleaseDate" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TechSupport_DataConnectionString %>" 
        SelectCommand="SELECT [ProductCode], [Name], [Version], [ReleaseDate] FROM [Products] WHERE ([ProductCode] = @ProductCode)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlProducts" Name="ProductCode" 
                PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>