zoukankan      html  css  js  c++  java
  • Webform中linq to sql多条件查询(小练习)

     

    多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件。。。

    aspx代码

    复制代码
     1 <body>
     2     <form id="form1" runat="server">
     3         
     4         <br />
     5         <asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label>
     6         <asp:TextBox ID="Gjz" runat="server" Font-Size="12px"></asp:TextBox>
     7 &nbsp;
     8         <asp:Label ID="Label2" runat="server" Text="价格:"></asp:Label>
     9 &nbsp;<asp:TextBox ID="price1" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
    10 &nbsp;
    11         <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
    12 &nbsp;
    13         <asp:TextBox ID="price2" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
    14 &nbsp;
    15         <asp:Button ID="select" runat="server" Text="查询" OnClick="select_Click" />
    16         <br />
    17         <br />
    18         <asp:Repeater ID="Repeater1" runat="server">
    19             <HeaderTemplate>
    20                 <table width="903" border="0" cellspacing="1" cellpadding="0" bgcolor="#6600FF">
    21                   <tr style="height:25px;">
    22                     <td width="260" bgcolor="#FFFFFF">名称</td>
    23                     <td width="160" bgcolor="#FFFFFF">上市时间</td>
    24                     <td width="160" bgcolor="#FFFFFF">油耗</td>
    25                     <td width="160" bgcolor="#FFFFFF">功率</td>
    26                     <td width="163" bgcolor="#FFFFFF">价格</td>
    27                   </tr>
    28             </HeaderTemplate>
    29 
    30             <ItemTemplate>
    31                  <tr style="height:25px;">
    32                     <td bgcolor="#FFFFFF"><%#Eval("Name") %></td>
    33                     <td bgcolor="#FFFFFF"><%#Eval("Time","{0:yyyy年MM月dd日}") %></td>
    34                     <td bgcolor="#FFFFFF"><%#Eval("Oil") %></td>
    35                     <td bgcolor="#FFFFFF"><%#Eval("Powers") %></td>
    36                     <td bgcolor="#FFFFFF"><%#Eval("Price") %></td>
    37                   </tr>
    38 
    39             </ItemTemplate>
    40 
    41             <FooterTemplate>
    42 
    43                  </table>
    44 
    45             </FooterTemplate>
    46 
    47 
    48         </asp:Repeater>
    49     </form>
    50 </body>
    复制代码

     aspx.cs代码:

    复制代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 
     8 public partial class test : System.Web.UI.Page
     9 {
    10     protected void Page_Load(object sender, EventArgs e)
    11     {
    12         if(!IsPostBack)
    13         {
    14          coenctDataContext _conext = new coenctDataContext();
    15            Repeater1.DataSource=  _conext.Car;
    16            Repeater1.DataBind();
    17         }
    18     }
    19 
    20 
    21     protected void select_Click(object sender, EventArgs e)
    22     {
    23         coenctDataContext _contest = new coenctDataContext();
    24 
    25         List<Car> list =  _contest.Car.ToList();
    26         string key =Gjz.Text.ToString();
    27         
    28         //判断第一个条件是否为空,如果为空不操作,去判断第二个条件看是否满足;不为空,继续 继续查询
    29         if (key != "")
    30         {
    31             list = list.Where(p => p.Name.Contains(key)).ToList();
    32             //关键字变原色,用foreach去查看集合中满足条件的字
    33             foreach(Car data in list )
    34             {
    35                 string s = data.Name.Replace(key, "<span style ='color:red;'>" + key + "</span>");
    36                 data.Name = s;
    37             }
    38         }
    39 
    40         //判断第二个条件
    41         string mi = price1.Text;//取text判断是否为空
    42         string ma = price2.Text;//取text判断是否为空
    43         
    44         //先判断第二个条件中,文本框中是否为空
    45         if(mi !="" && ma != "")
    46         {
    47             decimal min = Convert.ToDecimal(price1.Text);
    48             decimal max = Convert.ToDecimal(price2.Text);
    49 
    50            list=    list.Where(p=>p.Price.Value>=min && p.Price<=max).ToList();
    51 
    52         }
    53 
    54         //指定数据源显示出来
    55         Repeater1.DataSource = list;
    56         Repeater1.DataBind();
    57 
    58     }
    59 }
    复制代码
  • 相关阅读:
    Oracle-创建PDB
    win10怎么看IP地址、怎么看主机名
    怎么读dll文件
    Windows的注册表是什么?
    DbHelperOra类的相关链接(没看)
    C#中//注释和///注释的区别
    有关C#的序列化
    【智能指针 | 01】std::shared_ptr的使用教程
    coreump
    d
  • 原文地址:https://www.cnblogs.com/dcdgmjzhal/p/4877743.html
Copyright © 2011-2022 走看看