使用GridView ASP.NET的DropDownList

时间:2012-07-26 17:52:30

标签: c# asp.net gridview

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

  

SELECT [ID],[fName],[lName] FROM [cb1] WHERE([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>

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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