0

我有一个 DropDownList,它通过 ID、名字、姓氏数据绑定到我们的 SQL 服务器。然后我添加了一个 GridView 并将数据源配置到这个

从 [cb1] 选择 [ID]、[fName]、[lName] 在哪里([ID] = @ID)

基本上我想要它做的是当有人从下拉列表中选择名字时,它会在 GridView 中弹出 ID、名字和姓氏。

问题是,加载后 DropDownList 为空且没有数据,并且 GridView 也丢失了。

这是标记。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                SortExpression="ID" />
            <asp:BoundField DataField="fName" HeaderText="fName" SortExpression="fName" />
            <asp:BoundField DataField="lName" HeaderText="lName" SortExpression="lName" />
        </Columns>
    </asp:GridView>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="ID" DataValueField="ID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:comboConnectionString1 %>" 
        DeleteCommand="DELETE FROM [cb1] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [cb1] ([fName], [lName]) VALUES (@fName, @lName)" 
        ProviderName="<%$ ConnectionStrings:comboConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID)" 
        UpdateCommand="UPDATE [cb1] SET [fName] = @fName, [lName] = @lName WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="fName" Type="String" />
            <asp:Parameter Name="lName" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="ID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="fName" Type="String" />
            <asp:Parameter Name="lName" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
</p>
4

2 回答 2

1

问题是,加载后 DropDownList 为空且没有数据,并且 GridView 也丢失了。

当您的页面首次加载时,它会触发此 Sql 查询(SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID)以返回网格和组合使用的 sqldatasource1 的结果

表示不会返回任何结果,因为@ID 将为 null 或 0

所以你会有(SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID OR [ID] IS NOT NULL)

于 2012-07-26T18:04:51.853 回答
1
Table Contains id,Name,department and age


 Department :
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource2" DataTextField="Department" 
            DataValueField="Department">
        </asp:DropDownList>
        <br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:demoConnectionString %>" 
            SelectCommand="SELECT DISTINCT [Department] FROM [profile]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource3">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Department" HeaderText="Department" 
                    SortExpression="Department" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:demoConnectionString %>" 
            SelectCommand="SELECT * FROM [profile] WHERE ([Department] = @Department)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Department" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

Result:
![Result][1]


  [1]: http://i.stack.imgur.com/WrfP7.png
于 2012-07-26T18:09:10.593 回答