码迷,mamicode.com
首页 > 其他好文 > 详细

租房交集查询

时间:2015-08-20 01:20:02      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

网页界面

<title></title>
<script language="javascript">
function checkAll(ckall, ckname) {
var cks = document.getElementsByName(ckname);
for (var i = 0; i < cks.length; i++) {
cks[i].checked = ckall.checked;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

区域:<input id="ckAreaAll" type="checkbox" onclick="checkAll(this,‘ckarea‘)" />全选<br />
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<input id="ckArea_<%# Eval("Code") %>" name="ckarea" type="checkbox" value="<%# Eval("Code") %>"/><span><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
租赁类型:<asp:CheckBox ID="ckRentTypeAll" runat="server" Text="全选" />
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
</asp:CheckBoxList>
<br />
<br />
房屋类型:<input id="ckHouseTypeAll" type="checkbox" onclick="checkAll(this, ‘ckhousetype‘)" />全选<br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<br />
关键词:<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnFind" runat="server" Text="搜索" OnClick="btnFind_Click" />

<br />
<br />
<asp:Repeater ID="Repeater2" runat="server">
<HeaderTemplate>
<table width="100%" border="1">
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("KeyWord") %></td>
<td><%# Eval("Area") %></td>
<td><%# Eval("SquareMeter") %></td>
<td><%# Eval("Rent") %></td>
<td><%# Eval("RentType") %></td>
<td><%# Eval("HouseType") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>

</div>
</form>

 

 

代码界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
#region ==填充复选框的==
private void FillArea()
{
//准备数据
List<ItemData> list = new List<ItemData>();
List<string> temp = _Context.House.Select(p => p.Area).Distinct().ToList();
foreach (string s in temp)
{
ItemData data = new ItemData();
data.Code = s;
data.Name = s;
list.Add(data);
}
//界面显示
Repeater1.DataSource = list;
Repeater1.DataBind();
}
private void FillRentType()
{
//准备数据
List<ItemData> list = new List<ItemData>();
List<string> temp = _Context.House.Select(p => p.RentType).Distinct().ToList();
foreach (string s in temp)
{
ItemData data = new ItemData();
data.Code = s;
data.Name = s;
list.Add(data);
}
//界面显示
CheckBoxList1.DataSource = list;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "Code";
CheckBoxList1.DataBind();
}
private void FillHouseType()
{
//准备数据
List<string> temp = _Context.House.Select(p => p.HouseType).Distinct().ToList();

//界面显示
foreach (string s in temp)
{
Literal1.Text += "<input type=‘checkbox‘ id=" + s + " name=‘ckhousetype‘ value=‘" + s + "‘>" + s;
}


}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillArea();
FillRentType();
FillHouseType();
}
}

protected void btnFind_Click(object sender, EventArgs e)
{
#region ==组合查询==
//所选区的查询
var query1 = _Context.House.AsQueryable();
if (Request["ckarea"] != null)
{
string strarea = Request["ckarea"];
string[] areas = strarea.Split(‘,‘);
query1 = query1.Where(p => areas.Contains(p.Area));
}

//租赁类型查询
var query2 = _Context.House.AsQueryable();
List<string> rentTypes = new List<string>();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true)
{
rentTypes.Add(li.Value);
}
}
if (rentTypes.Count > 0)
{
query2 = query2.Where(p => rentTypes.Contains(p.RentType));
}
//房屋类型查询
var query3 = _Context.House.AsQueryable();
if (Request["ckhousetype"] != null)
{
string strhousetype = Request["ckhousetype"];
string[] houseTypes = strhousetype.Split(‘,‘);
query3 = query3.Where(p => houseTypes.Contains(p.HouseType));
}

//关键词查询
var query4 = _Context.House.AsQueryable();
if (txt.Text.Trim().Length > 0)
{
query4 = query4.Where(p => p.KeyWord.Contains(txt.Text));
}

//整合
var query = query1.Intersect(query2).Intersect(query3).Intersect(query4);

Repeater2.DataSource = query;
Repeater2.DataBind();
#endregion
}

租房交集查询

标签:

原文地址:http://www.cnblogs.com/tianxuan/p/4743898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!