下拉列表选择项目加载listview vb asp.net

时间:2014-02-18 13:48:58

标签: asp.net sql vb.net

我有一个搜索功能,可以使用sql加载列表视图。

我希望用户能够通过说“名称”进行搜索,然后下拉列表将填充所有具有该用户搜索名称的部门名称。然后单击下拉列表中的项目然后填充列表视图。

这是我到目前为止所拥有的

下拉

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"   DataTextField="DepartmentName" DataValueField="DepartmentName">
                                        </asp:DropDownList>




<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnectionConnectionString %>" SelectCommand="SELECT DISTINCT [DepartmentName], [ExtensionName], [Surname], [jobTitle], [FirstName], [Pager] FROM [UserInformation]" FilterExpression="DepartmentName LIKE '%{0}%' or FirstName + ' ' + Surname LIKE '%{0}%' or FirstName + ' ' + DepartmentName LIKE '%{0}%' or Surname + ' ' + DepartmentName LIKE '%{0}%' or FirstName + ' ' + Surname + ' ' + DepartmentName LIKE '%{0}%' or
                                FirstName + ' ' + Surname + ' ' + ExtensionName LIKE '%{0}%' or DepartmentName + ' ' + ExtensionName LIKE '%{0}%' or ExtensionName + ' ' + DepartmentName LIKE '%{0}%' or ExtensionName + ' ' + FirstName LIKE '%{0}%' or FirstName + ' ' + ExtensionName LIKE '%{0}%' or ExtensionName LIKE '%{0}%' or jobTitle LIKE '%{0}%' or Pager LIKE '%{0}%' or DepartmentName LIKE '{0}'">
                                <FilterParameters>


                                    <asp:ControlParameter Name="DepartmentName" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="ExtensionName" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="Surname" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="JobTitle" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="FirstName" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="Pager" ControlID="txtSearch1" ConvertEmptyStringToNull="false" PropertyName="Text" />
                                    <asp:ControlParameter Name="DepartmentName" ControlID="DropDownList1"  PropertyName="SelectedValue" Type="String" />

                                </FilterParameters>
                            </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

不确定您要问的是什么,但我认为这是您正在寻找的FilterExpression:

DepartmentName LIKE '%{0}%' OR
ExtensionName LIKE '%{1}%' OR
Surname LIKE '%{2}%' OR
JobTitle LIKE '%{3}%' OR
FirstName LIKE '%{4}%' OR
Pager LIKE '%{5}%' OR
DepartmentName LIKE '%{6}%'

在搜索'%string%'时,您不需要连接这些组合。

例如: 如果DepartmentName是“Office”,那么FirstName + ' ' + DepartmentName可能是“Bob Office”,并且在DepartmentName下拉列表/文本框中搜索“Office”时,两者都会返回相同的行。所以我简化了您的FilterExpression,然后我更改了索引({0})以匹配相应的ControlParameters。

修改

这是一种做你要求的方法......

<强> ASPX

<asp:TextBox ID="SearchBox" runat="server" Height="27px" OnTextChanged="SearchBox_TextChanged" />
<br />
<asp:DropDownList ID="DepartmentDDL" runat="server" DataSourceID="DepartmentSource" DataTextField="DepartmentName" />
<asp:SqlDataSource ID="DepartmentSource" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnectionConnectionString  %>" SelectCommand="SELECT DISTINCT * FROM [UserInformation] WHERE ([DepartmentName] LIKE '%' + @SearchParam+ '%') ORDER BY [DepartmentName]">
    <SelectParameters>
        <asp:FormParameter FormField="SearchBox" Name="SearchParam" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

<强> CS

protected void SearchBox_TextChanged(object sender, EventArgs e)
{
    DepartmentDDL.DataBind();
}

你可以添加一个按钮来绑定一个事件而不是SearchBox_TextChanged事件(在文本框中点击输入以便顺便触发它)。如果你正在寻找在你输入时过滤的东西,你需要多玩一点或采取不同的方法。