ASP.net DropDownList填充GridView

时间:2011-08-21 21:25:00

标签: asp.net gridview filter drop-down-menu

我正在尝试设置GridView来过滤属于特定Employee的凭据。下拉列表提供了员工列表,一旦选中,我希望gridview仅填充属于特定员工的条目。

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource2" DataKeyNames="Employee_ID">
        <Columns> 
            <asp:BoundField DataField="Fullname" HeaderText="Fullname" 
                            ReadOnly="True" SortExpression="Fullname" />
            <asp:BoundField DataField="Primary_Address" HeaderText="Primary_Address" 
                            SortExpression="Primary_Address" />
            <asp:BoundField DataField="Primary_Phone" HeaderText="Primary_Phone" 
                            SortExpression="Primary_Phone" />
            <asp:BoundField DataField="E_mail" HeaderText="E_mail" 
                            SortExpression="E_mail" />
            <asp:BoundField DataField="Degree" HeaderText="Degree" 
                            SortExpression="Degree" />
            <asp:BoundField DataField="Years_Experience" HeaderText="Years_Experience" 
                            SortExpression="Years_Experience" />
            <asp:BoundField DataField="Certifications" HeaderText="Certifications" 
                            SortExpression="Certifications" />
            <asp:BoundField DataField="Positions" HeaderText="Positions" 
                            SortExpression="Positions" />
            <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" 
                            InsertVisible="False" ReadOnly="True" 
                            SortExpression="Employee_ID" />
        </Columns>
    </asp:GridView>
</asp:Content>

1 个答案:

答案 0 :(得分:1)

您没有说明当前代码发生了什么,但我敢打赌,当您选择一名员工时,页面会重新加载并获得数据库中所有员工的列表,对吧?

如果这是真的,原因是因为同一个SqlDataSource绑定到DropDownList和GridView,而SqlDataSource的Select命令检索所有员工 - 没有WHERE标准来选择所需的员工。

我会使用2个SqlDataSources - 一个用于DropDownList,一个用于GridView。第二个SqlDataSource将有一个Select命令,用于根据DropDownList中的选择获取所需的员工信息。

您可以修改SqlDataSource2的SelectCommand以仅返回fullname和EmployeeID字段,因为您不需要DropDownList的其余部分:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT F_Name + ' ' + L_Name AS Fullname, Employee_ID FROM Employee"></asp:SqlDataSource>

在第二个SqlDataSource中,您需要向SelectParameters集合添加一个参数(EmployeeID),如下所示,并更新您的SelectCommand以获取参数。

    <asp:SqlDataSource ID="gvDataSource" runat="server"
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID WHERE Employee.EmployeeID = IsNull(@EmployeeID, EmployeeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownListID" 
                 ConvertEmptyStringToNull="true" Name="EmployeeID"
                 PropertyName="SelectedValue" />
        </SelectParameters>

然后将此SqlDataSource分配给GridView而不是第一个。

请注意,SqlDataSource没有对提交到参数中的值进行验证 - 这可能是安全威胁。

SqlDataSource.Select Method

SqlDataSource.SelectParameters Property