多列搜索条件GridView

时间:2018-12-05 23:45:56

标签: c# sql asp.net datagridview sqldatasource

我正在尝试使用TextBoxes和DropDownList指定的多个条件执行搜索。
我面临的问题是,如果所说的TextBoxes / DropDownList中包含值,我需要一种附加附加的AND LIKE命令的方法,不幸的是,按钮的行为不符合我的期望,它不显示任何数据网格。即使文本框为空白,该按钮也不会执行SELECT命令。

代码提示:

protected void queryButton_Click(object sender, EventArgs e)
{
    string Filter = "";

    if (resIDSearch.Text != "") { Filter = Filter + "ID LIKE '%" + resIDSearch.Text + "%' AND "; }
    if (DFirstName.Text != "") { Filter = Filter + "FirstName LIKE '%" + DFirstName.Text + "%' AND "; }
    if (DLastName.Text != "") { Filter = Filter + "LastName LIKE '%" + DLastName.Text + "%' AND "; }
    if (contactNumSearch.Text != "") { Filter = Filter + "ContactNumber LIKE '%" + contactNumSearch.Text + "%' AND "; }
    if (lotNumSearch.Text != "") { Filter = Filter + "LotNumber LIKE '%" + lotNumSearch.Text + "%' AND "; }
    if (streetSearch.Text != "") { Filter = Filter + "StreetName LIKE '%" + streetSearch.Text + "%' AND "; }
    if (villiageSearch.Text != "") { Filter = Filter + "VilliageName LIKE '%" + villiageSearch.Text + "%' AND "; }
    if (vTypeBox.SelectedIndex != 0) { Filter = Filter + "VehicleType LIKE '%" + vTypeBox.SelectedItem.Text + "%' AND "; }
    if (statusBox.SelectedIndex != 0) { Filter = Filter + "StatusID LIKE '%" + statusBox.SelectedValue + "%' AND "; }
    if (DriIDSearch.Text != "") { Filter = Filter + "UserID LIKE '%" + DriIDSearch.Text + "%' AND "; }
    if (resDateSearch.Text != "") { Filter = Filter + "ReservationDate LIKE '%" + resDateSearch.Text + "%' AND "; }
    if (destSearch.Text != "") { Filter = Filter + "Destination LIKE '%" + destSearch.Text + "%' AND "; }
    if (pickUpSearch.Text != "") { Filter = Filter + "PickupLocation LIKE '%" + pickUpSearch.Text + "%' AND "; }

    if (Filter.Length > 0)
    {
        string FinalFilter = Filter.Remove(Filter.Length - 4, 3);
        resDataSource.FilterExpression = FinalFilter;
    }
    else {
        reservationTable.DataBind();
    }
}

.NET脚本:

<asp:GridView OnRowDataBound="reservationTable_RowDataBound" DataSourceID="resDataSource" CssClass="table table-bordered dataTable text-center" Width="100%" ID="reservationTable" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Reservation ID" />
        <asp:BoundField DataField="UserID" HeaderText="Driver ID" />
        <asp:BoundField DataField="FirstName" HeaderText="Driver First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Driver Last Name" />
        <asp:BoundField DataField="StatusID" HeaderText="Status" />
        <asp:BoundField DataField="ContactNumber" HeaderText="Customer Contact #" />
        <asp:BoundField DataField="LotNumber" HeaderText="Lot Number" />
        <asp:BoundField DataField="StreetName" HeaderText="Street Name" />
        <asp:BoundField DataField="VilliageName" HeaderText="Villiage" />
        <asp:BoundField DataField="Note" HeaderText="Additional Information" />
        <asp:BoundField DataField="VehicleType" HeaderText="Vehicle Requested" />
        <asp:BoundField DataField="ReservationDate" HeaderText="Reservation Date" />
        <asp:BoundField DataField="Destination" HeaderText="Destination" />
        <asp:BoundField DataField="PickupLocation" HeaderText="Pick up" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="resDataSource"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:SiteDatabase%>"
    ProviderName="<%$ ConnectionStrings:SiteDatabase.providerName%>"
    SelectCommand="SELECT * FROM Reservation"></asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

Aspx:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ”                                         ProviderName =“ <%$ ConnectionStrings:SiteDatabase.providerName%>”                                         SelectCommand =“从预订中选择*”>

        string Filter = "";
        string command = "SELECT * FROM Reservation WHERE";

        if (resIDSearch.Text != "") { Filter = Filter + " ID LIKE '%" + resIDSearch.Text + "%' AND"; }
        if (DFirstName.Text != "") { Filter = Filter + " FirstName LIKE '%" + DFirstName.Text + "%' AND"; }
        if (DLastName.Text != "") { Filter = Filter + " LastName LIKE '%" + DLastName.Text + "%' AND"; }
        if (contactNumSearch.Text != "") { Filter = Filter + " ContactNumber LIKE '%" + contactNumSearch.Text + "%' AND"; }
        if (lotNumSearch.Text != "") { Filter = Filter + " LotNumber LIKE '%" + lotNumSearch.Text + "%' AND"; }
        if (streetSearch.Text != "") { Filter = Filter + " StreetName LIKE '%" + streetSearch.Text + "%' AND"; }
        if (villiageSearch.Text != "") { Filter = Filter + " VilliageName LIKE '%" + villiageSearch.Text + "%' AND"; }
        if (vTypeBox.SelectedIndex != 0) { Filter = Filter + " VehicleType LIKE '%" + vTypeBox.SelectedItem.Text + "%' AND"; }
        if (statusBox.SelectedIndex != 0) { Filter = Filter + " StatusID LIKE '%" + statusBox.SelectedValue + "%' AND"; }
        if (DriIDSearch.Text != "") { Filter = Filter + " UserID LIKE '%" + DriIDSearch.Text + "%' AND"; }
        if (resDateSearch.Text != "") { Filter = Filter + " ReservationDate LIKE '%" + resDateSearch.Text + "%' AND"; }
        if (destSearch.Text != "") { Filter = Filter + " Destination LIKE '%" + destSearch.Text + "%' AND"; }
        if (pickUpSearch.Text != "") { Filter = Filter + " PickupLocation LIKE '%" + pickUpSearch.Text + "%' AND"; }

        if (Filter.Length > 0)
        {
            reservationTable.DataSource = resDataSource;
            string FinalFilter = Filter.Remove(Filter.Length - 3);
            resDataSource.SelectCommand = command + FinalFilter;
            reservationTable.DataBind();

        }
        else {
            reservationTable.DataBind();
        }

我实际上是通过更改后面代码中的sqldatasource Select命令来解决它的,并取得了一些成功,而且效果很好!像我一样跟随每个文本间距。