LINQ to SQL multi-conditional query in WebForm (small exercise)

Source: Internet
Author: User

Multi-Criteria Query: Judge by article, from the first condition to judge, if satisfied, take out into the collection, and then query the second condition from the collection ...

aspx code :

1<body>2<form id="Form1"runat="Server">3         4<br/>5<asp:label id="Label1"runat="Server"text="Key Words:"></asp:Label>6<asp:textbox id="GJZ"runat="Server"Font-size="12px"></asp:TextBox>7&nbsp;8<asp:label id="Label2"runat="Server"text="Price:"></asp:Label>9&nbsp;<asp:textbox id="Price1"runat="Server"Font-size="12px"Width="52px"></asp:TextBox>Ten&nbsp; One<asp:label id="Label3"runat="Server"text="to the"></asp:Label> A&nbsp; -<asp:textbox id="Price2"runat="Server"Font-size="12px"Width="52px"></asp:TextBox> -&nbsp; the<asp:button id="Select"runat="Server"text="Enquiry"onclick="Select_click"/> -<br/> -<br/> -<asp:repeater id="Repeater1"runat="Server"> +<HeaderTemplate> -<table width="903"Border="0"cellspacing="1"cellpadding="0"Bgcolor="#6600FF"> +<tr style="height:25px;"> A&LT;TD width="260"Bgcolor="#FFFFFF"> Name </td> at&LT;TD width=" the"Bgcolor="#FFFFFF"> Time to Market </td> -&LT;TD width=" the"Bgcolor="#FFFFFF"> Fuel Consumption </td> -&LT;TD width=" the"Bgcolor="#FFFFFF"> Power </td> -&LT;TD width="163"Bgcolor="#FFFFFF"> Price </td> -</tr> -</HeaderTemplate> in  -<ItemTemplate> to<tr style="height:25px;"> +&LT;TD bgcolor="#FFFFFF"><% #Eval ("Name")%></td> -&LT;TD bgcolor="#FFFFFF"><% #Eval (" Time","{0:yyyy mm month DD Day}")%></td> the&LT;TD bgcolor="#FFFFFF"><% #Eval (" Oil")%></td> *&LT;TD bgcolor="#FFFFFF"><% #Eval ("Powers")%></td> $&LT;TD bgcolor="#FFFFFF"><% #Eval (" Price")%></td>Panax Notoginseng</tr> -  the</ItemTemplate> +  A<FooterTemplate> the  +</table> -  $</FooterTemplate> $  -  -</asp:Repeater> the</form> -</body>

Aspx.cs Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8  Public Partial classTest:System.Web.UI.Page9 {Ten     protected voidPage_Load (Objectsender, EventArgs e) One     { A         if(!IsPostBack) -         { -Coenctdatacontext _conext =NewCoenctdatacontext (); theRepeater1.datasource=_conext. Car; - Repeater1.databind (); -         } -     } +  -  +     protected voidSelect_click (Objectsender, EventArgs e) A     { atCoenctdatacontext _contest =NewCoenctdatacontext (); -  -list<car> list =_contest. Car.tolist (); -         stringKey =Gjz.Text.ToString (); -          -         //determine whether the first condition is empty, if it is empty, to determine whether the second condition is satisfied; not empty, continue to query in         if(Key! ="") -         { toList = list. Where (p =P.name.contains (key)). ToList (); +             //Keyword to change the primary color, use foreach to see the words that meet the criteria in the collection -             foreach(Car datainchlist) the             { *                 strings = data. Name.replace (Key,"<span style = ' color:red; ' >"+ key +"</span>"); $Data. Name =s;Panax Notoginseng             } -         } the  +         //judging the second condition A         stringMi = Price1. Text;//take text to determine if it is empty the         stringMa = price2. Text;//take text to determine if it is empty +          -         //first determine whether the text box is empty in the second condition $         if(Mi! =""&& Ma! ="") $         { -             decimalMin =Convert.todecimal (Price1. Text); -             decimalMax =Convert.todecimal (Price2. Text); the  -list= list. Where (p=>p.price.value>=min && p.price<=max). ToList ();Wuyi  the         } -  Wu         //Specify the data source to display -Repeater1.datasource =list; About Repeater1.databind (); $  -     } -}

LINQ to SQL multi-conditional query in WebForm (small exercise)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.