使用SqlDataSource过滤GridView,DropDownLists无法正常工作

时间:2012-03-29 06:44:31

标签: c# asp.net sql sqldatasource

我用gridview格式制作了一个搜索功能。我有1个文本框,2个下拉列表和一个搜索按钮。我的文本框用于搜索医生的姓氏,第一个和第二个下拉列表用于过滤他们所属的省或城市以及执行搜索按钮。

当我搜索医生的姓氏时,文本框正在工作,但下拉菜单没有。

这是我的代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
              AllowSorting="True" AutoGenerateColumns="False" BackColor="White" 
              BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
              DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical">
    <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
            <asp:BoundField DataField="DOCTOR_FIRSTNAME" HeaderText="DOCTOR_FIRSTNAME" 
                 SortExpression="DOCTOR_FIRSTNAME" />
            <asp:BoundField DataField="DOCTOR_MI" HeaderText="DOCTOR_MI" 
                 SortExpression="DOCTOR_MI" />
            <asp:BoundField DataField="DOCTOR_LASTNAME" HeaderText="DOCTOR_LASTNAME" 
                 SortExpression="DOCTOR_LASTNAME" />
            <asp:BoundField DataField="ROOM" HeaderText="ROOM" SortExpression="ROOM" />
            <asp:BoundField DataField="SCHEDULE" HeaderText="SCHEDULE" 
                 SortExpression="SCHEDULE" />
            <asp:BoundField DataField="SPECIALIZATION" HeaderText="SPECIALIZATION" 
                 SortExpression="SPECIALIZATION" />
            <asp:BoundField DataField="PROVINCE_NAME" HeaderText="PROVINCE_NAME" 
                 SortExpression="PROVINCE_NAME" />
            <asp:BoundField DataField="CITY_NAME" HeaderText="CITY_NAME" 
                 SortExpression="CITY_NAME" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="Gray" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MedicolConnectionString %>" 

        SelectCommand="SELECT 
            emed_accredited_providers.DOCTOR_FIRSTNAME, 
            emed_accredited_providers.DOCTOR_MI, 
            emed_accredited_providers.DOCTOR_LASTNAME, 
            emed_doctors_hospitals.ROOM, 
            emed_doctors_hospitals.SCHEDULE, 
            emed_accredited_providers.SPECIALIZATION, 
            emed_province.PROVINCE_NAME, 
            emed_city.CITY_NAME FROM emed_city 
            INNER JOIN emed_province ON emed_city.PROVINCE_CODE = 
            emed_province.PROVINCE_CODE INNER JOIN 
            emed_doctors_hospitals ON emed_city.CITY_CODE = 
            emed_doctors_hospitals.CITY_CODE INNER JOIN 
            emed_accredited_providers ON 
            emed_doctors_hospitals.DOCTOR_CODE = 
            emed_accredited_providers.DOCTOR_CODE WHERE 
            (emed_accredited_providers.DOCTOR_LASTNAME LIKE '%' + 
            @DOCTOR_LASTNAME + '%') OR 
            (emed_doctors_hospitals.PROVINCE_CODE = @PROVINCE_CODE) 
            ORDER BY emed_accredited_providers.DOCTOR_FIRSTNAME"
        <SelectParameters>
            <asp:ControlParameter ControlID="TextBox1" 
                Name="DOCTOR_LASTNAME" 
                PropertyName="Text" />
            <asp:ControlParameter ControlID="DropDownList1" 
                Name="PROVINCE_CODE" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource2" DataTextField="PROVINCE_NAME" 
        DataValueField="PROVINCE_NAME">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MedicolConnectionString %>" 
        SelectCommand="SELECT [PROVINCE_NAME] FROM [emed_province] ORDER BY [PROVINCE_NAME] ASC">
    </asp:SqlDataSource>
    <asp:DropDownList ID="DropDownList2" runat="server">
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" Text="Search" />

1 个答案:

答案 0 :(得分:0)

使用DataValueField=PROVINCE_CODE,因为您正在使用where子句中的下拉列表中的值来获取记录。

目前,PROVINCE_NAME将转到搜索查询,而不是PROVINCE_CODE

<asp:DropDownList ID="DropDownList1" runat="server"  
        DataSourceID="SqlDataSource2" DataTextField="PROVINCE_NAME"  
        DataValueField="PROVINCE_NAME"> 


使用它而不是上面的那个:

<asp:DropDownList ID="DropDownList1" runat="server"  
        DataSourceID="SqlDataSource2" DataTextField="PROVINCE_NAME"  
        DataValueField="PROVINCE_CODE"> 

此外,您需要从表中提取[PROVINCE_CODE]以将其绑定到下拉列表。

<asp:SqlDataSource ID="SqlDataSource2" runat="server"  
        ConnectionString="<%$ ConnectionStrings:MedicolConnectionString %>"  
        SelectCommand="SELECT [PROVINCE_CODE],[PROVINCE_NAME] FROM [emed_province] ORDER BY [PROVINCE_NAME] ASC">