1

有 3 个父子下拉列表,选择第 3 个下拉列表后,DetailsView 将显示所有选择的 3 个下拉列表的结果。第一个是 ServerName,第二个是 Instance,第三个是 DatabaseName。选择服务器名称的第一个下拉列表后,实例值的新列表出现在第二个下拉列表中。当我在第二个下拉列表中选择任何内容时。有一条错误消息说,“必须声明标量变量“@ServerName””。我不明白这是什么意思,请帮忙。这里有一个下拉列表代码,

<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>

<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
    <SelectParameters>
        <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

<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">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName AND [Instance] = @Instance">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

然后我将那些下拉列表函数传递给 SQLDataSource2 附带的 DetailsView,在 SQLDataSource2 中的 SelectCommand 上,我写了 SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = @DatabaseName) AND ([Instance] = @Instance) AND ([ServerName] = @ServerName))"

然后我添加 SelectParameter 代码,

<SelectParameters>
    <asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
    <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
    <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
4

2 回答 2

1

在 SqlDataSource3 中,您的查询采用两个参数:@ServerName 和 @Instance。但是在选择参数中,你只定义了@Instance。是的,您在上一个数据源中定义了@ServerName,但在这个数据源中没有。

于 2012-11-30T18:12:13.710 回答
0

我从 Jay 最近提到的内容中修复了另一个问题,我在实例和数据库下拉列表代码上修复了它,我修复了什么并且它工作正常。

<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
    <br />
    <asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
        <SelectParameters>
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

    <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">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName AND [Instance] = @Instance">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
于 2012-11-30T18:23:51.320 回答