ASP.net如何使用dropdownlist从数据库中检索数据

时间:2014-12-28 21:37:58

标签: asp.net vb.net

我有一个按钮,下拉列表和gridview。当我从下拉列表中选择任何选项并单击按钮时,我希望这样。然后将相关数据显示到gridview .i有一个问题,数据显示在gridview中,但gridview d使用按钮

我的代码是

<form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="E_NAME" DataValueField="E_ID">


        </asp:DropDownList>

        <asp:Button ID="Button1" runat="server" Text="Button" />

    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [E_ID], [E_NAME] FROM [EMP_DETAIL]">
    </asp:SqlDataSource>
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [E_ID], [E_NAME], [E_CITY], [E_AGE], [E_DEPARTMENT] FROM [EMP_DETAIL] WHERE ([E_ID] = @E_ID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="E_ID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="E_ID" DataSourceID="SqlDataSource2">
        <Columns>
            <asp:BoundField DataField="E_ID" HeaderText="E_ID" ReadOnly="True" 
                SortExpression="E_ID" />
            <asp:BoundField DataField="E_NAME" HeaderText="E_NAME" 
                SortExpression="E_NAME" />
            <asp:BoundField DataField="E_CITY" HeaderText="E_CITY" 
                SortExpression="E_CITY" />
            <asp:BoundField DataField="E_AGE" HeaderText="E_AGE" SortExpression="E_AGE" />
            <asp:BoundField DataField="E_DEPARTMENT" HeaderText="E_DEPARTMENT" 
                SortExpression="E_DEPARTMENT" />
        </Columns>
    </asp:GridView>
    </form>

1 个答案:

答案 0 :(得分:0)

将下拉列表的AutoPostBack属性设置为False或将其删除: -

 <asp:DropDownList ID="DropDownList1" runat="server" 
      DataSourceID="SqlDataSource1" DataTextField="E_NAME" DataValueField="E_ID">
 </asp:DropDownList>

由于您需要点击按钮的结果,因此在点击按钮并在下拉列表中选择所选值后,表格将会刷新。