WB 练习 CheckBoxList 控件 去重显示 点击查询
前台HTML代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SouSuo.aspx.cs" Inherits="SouSuo" %> <! DOCTYPE html> < 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"> < div > < asp:Label ID="Label1" runat="server" Text="区域:"></ asp:Label > < asp:CheckBox ID="ckQuAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" /> < br /> < asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" RepeatDirection="Horizontal"> </ asp:CheckBoxList > < br /> < br /> < asp:Label ID="Label2" runat="server" Text="租赁类型:"></ asp:Label > < asp:CheckBox ID="ckZuAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" /> < br /> < asp:CheckBoxList ID="CheckBoxList2" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" OnCallingDataMethods="CheckBoxList2_CallingDataMethods" RepeatDirection="Horizontal"> </ asp:CheckBoxList > < br /> < br /> < asp:Label ID="Label3" runat="server" Text="房屋类型:"></ asp:Label > < asp:CheckBox ID="ckFangAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="CheckBox3_CheckedChanged" /> < br /> < asp:CheckBoxList ID="CheckBoxList3" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" RepeatDirection="Horizontal"> </ asp:CheckBoxList > < br /> < asp:Label ID="Label4" runat="server" Text="关键字:"></ asp:Label > < asp:TextBox ID="TextBox1" runat="server"></ asp:TextBox > < br /> < br /> < asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="搜索" /> < br /> < br /> < br /> < asp:Repeater ID="Repeater1" runat="server"> < HeaderTemplate > < table width="1000" border="0" cellspacing="1" cellpadding="1" bgcolor="#6600FF"> < tr > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF">关键字</ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF">区域</ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF">使用面积</ td > < td width="100" align="center" valign="middle" bgcolor="#FFFFFF">租金</ td > < td width="100" align="center" valign="middle" bgcolor="#FFFFFF">租赁类型</ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF">房屋类型</ td > </ tr > </ HeaderTemplate > < FooterTemplate > </ table > </ FooterTemplate > < ItemTemplate > < tr > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("KeyWord") %></ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Area") %></ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("SquareMeter") %></ td > < td width="100" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Rent") %></ td > < td width="100" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("RentType") %></ td > < td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("HouseType") %></ td > </ tr > </ ItemTemplate > </ asp:Repeater > </ div > </ form > </ body > </ html > |
后台C#代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; public partial class SouSuo : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { TestDataContext context = new TestDataContext(); Repeater1.DataSource = context.House; Repeater1.DataBind(); //去重显示 方法一 //查出这个表 放在一个集合里面 //var query = context.House; //foreach (House data in query) //{ // ListItem item = new ListItem(); // item.Text = data.Area; // //去重显示 判断早的像是不是已经存在 // if (!CheckBoxList1.Items.Contains(item)) // { // //添加 // CheckBoxList1.Items.Add(item); // } //} //去重显示方法二: //Select选取这条数据 进行去重 放到一个集合里面 List< string > list = context.House.Select(p => p.Area).Distinct().ToList(); foreach ( string text in list) { ListItem item = new ListItem(); item.Text = text; CheckBoxList1.Items.Add(item); } List< string > listR = context.House.Select(p => p.RentType).Distinct().ToList(); foreach ( string text in listR) { ListItem item = new ListItem(); item.Text = text; CheckBoxList2.Items.Add(item); } List< string > listH = context.House.Select(p => p.HouseType).Distinct().ToList(); foreach ( string text in listH) { ListItem item = new ListItem(); item.Text = text; CheckBoxList3.Items.Add(item); } } } protected void CheckBoxList1_SelectedIndexChanged( object sender, EventArgs e) { } protected void CheckBox1_CheckedChanged( object sender, EventArgs e) { foreach (ListItem item in CheckBoxList1.Items) { item.Selected = ckQuAll.Checked; } } protected void CheckBoxList2_CallingDataMethods( object sender, CallingDataMethodsEventArgs e) { } protected void CheckBox2_CheckedChanged( object sender, EventArgs e) { foreach (ListItem item in CheckBoxList2.Items) { item.Selected = ckZuAll.Checked; } } protected void CheckBox3_CheckedChanged( object sender, EventArgs e) { foreach (ListItem item in CheckBoxList3.Items) { item.Selected = ckFangAll.Checked; } } protected void Button1_Click( object sender, EventArgs e) { TestDataContext context = new TestDataContext(); List<House> list = context.House.ToList(); //造集合 ArrayList listArea = new ArrayList(); ArrayList listZu = new ArrayList(); ArrayList listHouse = new ArrayList(); //区域筛选 //有选中项 并且不全选 if (CheckBoxList1.SelectedIndex >= 0 && !ckQuAll.Checked) { //取出里面的选中值 foreach (ListItem item in CheckBoxList1.Items) { //如果被选中 取出里面的值 给到一个集合 if (item.Selected) { listArea.Add(item.Text); } } //listArea是区域集合 包含这条数据Area的区域 list = list.Where(p => listArea.Contains(p.Area)).ToList(); } //租赁类型筛选 //有选中项 并且不全选 if (CheckBoxList2.SelectedIndex >= 0 && !ckZuAll.Checked) { //取出里面的选中值 foreach (ListItem item in CheckBoxList2.Items) { //如果被选中 取出里面的值 给到一个集合 if (item.Selected) { listZu.Add(item.Text); } } //listArea是区域集合 包含这条数据Area的区域 list = list.Where(p => listZu.Contains(p.RentType)).ToList(); } //房屋类型筛选 //有选中项 并且不全选 if (CheckBoxList3.SelectedIndex >= 0 && !ckFangAll.Checked) { //取出里面的选中值 foreach (ListItem item in CheckBoxList3.Items) { //如果被选中 取出里面的值 给到一个集合 if (item.Selected) { listHouse.Add(item.Text); } } //listArea是区域集合 包含这条数据Area的区域 list = list.Where(p => listHouse.Contains(p.HouseType)).ToList(); } //关键字筛选 string KeyWord = TextBox1.Text; if (KeyWord != "" ) { list = list.Where(p => p.KeyWord.Contains(KeyWord)).ToList(); } Repeater1.DataSource = list; Repeater1.DataBind(); } } |
网页显示:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
· 重磅发布!DeepSeek 微调秘籍揭秘,一键解锁升级版全家桶,AI 玩家必备神器!