• 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
  • 相关阅读:
    开始制作国产的 scratch
    七个对我最重要的职业建议(译文)
    不要在功能上竞争
    来看看机智的前端童鞋怎么防盗
    悬疑塑成天才?
    Linux常用命令之重启关机命令
    Linux常用命令之网络命令
    Linux常用命令之文件搜索命令
    Linux常见命令之权限管理命令
    Linux常见命令之文件处理命令
  • 原文地址:https://www.cnblogs.com/hclyz/p/6899643.html
Copyright © 2020-2023  润新知