linq实现分页加组合查询

时间:2022-03-07 23:21:20

  前台aspx:

<body>
<form id="form1" runat="server">
<div>
车名:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
品牌:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
价格:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>=</asp:ListItem>
<asp:ListItem>></asp:ListItem>
<asp:ListItem><</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="查询" />


<table style="width: 100%; text-align: center;">
<tr style="background-color: navy; color: white;">
<td>编号</td>
<td>车名</td>
<td>品牌</td>
<td>时间</td>
<td>耗油量</td>
<td>功率</td>
<td>排气量</td>
<td>价格</td>
<td>图片</td>
</tr>


<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>

<tr style="background-color: gray; color: white;">
<td><%#Eval("code") %></td>
<td><%#Eval("name") %></td>
<td><%#Eval("brand") %></td>
<td><%#Eval("time") %></td>
<td><%#Eval("oil") %></td>
<td><%#Eval("powers") %></td>
<td><%#Eval("exhaust") %></td>
<td><%#Eval("price") %></td>
<td><%#Eval("pic") %></td>
</tr>

</ItemTemplate>
</asp:Repeater>
</table>

当前第【
<asp:Label ID="Label1_now" runat="server" Text="1"></asp:Label>】页
<asp:Button ID="Button_first" runat="server" Text="首页" />
<asp:Button ID="Button_prev" runat="server" Text="上一页" />
<asp:Button ID="Button_next" runat="server" Text="下一页" />
<asp:Button ID="Button_last" runat="server" Text="末页" />
最大页数【
<asp:Label ID="Label1" runat="server" Text="0"></asp:Label>





</div>
</form>
</body>


后台cs:

public partial class _Default : System.Web.UI.Page
{
int count = 5;
int page = 1;
int max = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DatamydbDataContext con = new DatamydbDataContext())
{
Repeater1.DataSource
= con.Car.Take(count);
Repeater1.DataBind();
Label1.Text
= Convert.ToInt32(Math.Ceiling(con.Car.Count() * 1.0 / count)).ToString();
}
}

Button_prev.Click
+= Button_prev_Click;
Button_next.Click
+= Button_next_Click;
Button1.Click
+= Button1_Click;
Button_first.Click
+= Button_first_Click;
Button_last.Click
+= Button_last_Click;

}

void Button_last_Click(object sender, EventArgs e)
{
page
= Convert.ToInt32(Label1.Text);
select();
}

void Button_first_Click(object sender, EventArgs e)
{
page
= 1;
select();
}

void Button1_Click(object sender, EventArgs e)
{
page
= 1;
select();

}

void Button_next_Click(object sender, EventArgs e)
{
page
= Convert.ToInt32(Label1_now.Text) + 1;
select();
}

void Button_prev_Click(object sender, EventArgs e)
{
page
= Convert.ToInt32(Label1_now.Text) - 1;
select();
}

public void select()
{
using (DatamydbDataContext con = new DatamydbDataContext())
{
IQueryable
<Car> clist = con.Car.AsQueryable();
if (TextBox1.Text.Trim().Length > 0)
{
clist
= clist.Where(r=>r.Name.Contains(TextBox1.Text));
}
if (TextBox2.Text.Trim().Length > 0)
{
clist
= clist.Where(r=>r.Brand.Contains(TextBox2.Text));
}
if (TextBox3.Text.Trim().Length > 0)
{
decimal price = Convert.ToDecimal(TextBox3.Text);
string st = DropDownList1.SelectedValue;
if (st == "=")
{ clist
= clist.Where(r=>r.Price==price); }
if (st == ">")
{ clist
= clist.Where(r => r.Price > price); }
if (st == "<")
{ clist
= clist.Where(r => r.Price < price); }

}
if (page < 1)
{
page
++;
return;
}
if (page > Convert.ToInt32(Math.Ceiling(clist.Count() * 1.0 / count)))
{
page
--;
return;
}
Repeater1.DataSource
= clist.Skip((page - 1) * count).Take(count);
Repeater1.DataBind();
Label1_now.Text
= page.ToString();
max
= Convert.ToInt32(Math.Ceiling(clist.Count() * 1.0 / count));
Label1.Text
= max.ToString();
}

}

}

 

相关文章