0

我使用 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,它从表数据库中绑定字段。在 SQLDataSource1 中,在 SelectCommand 上,我键入:

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

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

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

1 回答 1

0

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

我想这就是你想要的。请记住,当您为第二个下拉列表和详细信息视图设置数据源时,您必须确保使用向导创建 where 子句,以便两者都从下拉列表控件中获取主键。

例如,sqldatasource1(参见代码底部)的 ControlID 设置为“ddlProducts”,PropertyName 设置为“SelectedValue”。使用 sqlDataSource1 的详细信息视图使用 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>
于 2013-02-12T23:19:49.587 回答