Webform(条件查询)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        名称:<asp:TextBox ID="name" runat="server"></asp:TextBox>
        油耗:<asp:DropDownList ID="oil" runat="server">
            <asp:ListItem Text="大于" Value=">"></asp:ListItem>
            <asp:ListItem Text="小于" Value="<"></asp:ListItem>
            <asp:ListItem Text="等于" Value="="></asp:ListItem>
            <asp:ListItem Text="大于等于" Value=">="></asp:ListItem>
            <asp:ListItem Text="小于等于" Value="<="></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="oiltext" runat="server"></asp:TextBox>
        价格:<asp:DropDownList ID="price" runat="server">
            <asp:ListItem Text="20万至30万" Value="price>=20 and price<=30"></asp:ListItem>
            <asp:ListItem Text="30万至40万" Value="price>=30 and price<=40"></asp:ListItem>
            <asp:ListItem Text="大于40万" Value="price>=40"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="查询" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <table style=" 100%; background-color: blue; text-align: center;">
            <tr style="color: white;">
                <td>编号</td>
                <td>名称</td>
                <td>油耗</td>
                <td>马力</td>
                <td>排量</td>
                <td>价格</td>
            </tr>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <tr style="background-color: white;">
                        <td><%#Eval("Ids") %></td>
                        <td><%#Eval("Name") %></td>
                        <td><%#Eval("Oil") %></td>
                        <td><%#Eval("Powers") %></td>
                        <td><%#Eval("Exhaust") %></td>
                        <td><%#Eval("Price") %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>

    </form>
</body>
</html>
条件查询.aspx
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = new CarData().SelectAll();
            Repeater1.DataBind();
        }
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;
        string tsql = "select*from Car";
        Hashtable hs = new Hashtable();
        if (name.Text.Length > 0)
        {
            tsql += " where name like @a";
            hs.Add("@a", "%" + name.Text.Trim() + "%");
            count++;
        }
        if (oiltext.Text.Length > 0)
        {
            if (count > 0)
            {
                tsql += " and oil "+oil.SelectedValue +"@b";
            }
            else
            {
                tsql += " where oil " + oil.SelectedValue + "@b";
            }
            hs.Add("@b",  oiltext.Text.Trim());
            count++;
        }
        if (price.SelectedValue != null)
        {
            if (count > 0)
            {
                tsql += " and " + price.SelectedValue;
            }
            else
            {
                tsql += " where " + price.SelectedValue;
            }
           
            count++;
        }

        Repeater1.DataSource = new CarData().SelectAll(tsql,hs);
        Repeater1.DataBind();
    }
}
条件查询.cs
原文地址:https://www.cnblogs.com/hclyz/p/6899643.html