• WB 练习 CheckBoxList 控件 去重显示 点击查询


    前台HTML代码:

    <%@ 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#代码:

    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();
        }
    }
    

      网页显示:

  • 相关阅读:
    request和response使用
    oracle_to_char
    oracl_LTRIM_RITRIM
    convert
    jdbc
    oracle_trunc
    [python]glob模块中的glob()函数为什么返回空列表??
    win10 anaconda+tensorflow+keras
    Golang学习:sublime text3配置golang环境
    2018/12/05学习笔记
  • 原文地址:https://www.cnblogs.com/zhuxu/p/5073585.html
Copyright © 2020-2023  润新知