linq to sql 学习(6)linq的分页实例
来源:互联网 发布:淘宝上买ae片头模板 编辑:程序博客网 时间:2024/05/17 04:50
前面我们学习完了LINQ to SQL 的增删改查,对LINQ应该有比较熟悉的了解,这节我们接之前的 linq to sql 学习(4)增删改查实例 来继续学习关于LINQ的分页。
①,我们首先来改造下List.aspx页面的代码:(主要是增加了分页的几个按钮)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="LinqDemo.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="/jquery/jquery-1.4.1.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<table width="70%" border="1">
<tr>
<td>
id:<asp:TextBox ID="txt_id" runat="server"></asp:TextBox>
</td>
<td>
名称<asp:TextBox ID="txt_proname" runat="server"></asp:TextBox>
</td>
<td>
数量:<asp:TextBox ID="txt_insertcount" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnQuery" runat="server" OnClick="btnQuery_Click" Text="查询" />
<asp:Button ID="btndelete" runat="server" Text="删除" OnClick="btndelete_Click" />
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="修改" OnClick="btnUpdate_Click" />
</td>
</tr>
<tr>
<td colspan="4">
<asp:UpdatePanel runat="server" ID="up">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkall" AutoPostBack="true" OnCheckedChanged="chkall_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chk" runat="server" value='<%# Eval("id") %>' />
</ItemTemplate>
<ItemStyle Width="1px" />
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="proname" HeaderText="名称" />
<asp:BoundField DataField="insertday" HeaderText="入货时间" />
<asp:BoundField DataField="insertcount" HeaderText="入货数量" />
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:LinkButton runat="server" ID="lbFirstPage" OnClick="lbFirstPage_Click">第一页</asp:LinkButton>
<asp:LinkButton runat="server" ID="lbFrontPage" OnClick="lbFrontPage_Click">上一页</asp:LinkButton>
<asp:LinkButton runat="server" ID="lbNextPage" OnClick="lbNextPage_Click">下一页</asp:LinkButton>
<asp:LinkButton runat="server" ID="lbEndPage" OnClick="lbEndPage_Click">最后一页</asp:LinkButton>
总条数:<asp:Label ID="lblAllCount" runat="server" Text=""></asp:Label>
当前页:<asp:Label ID="lblCurrentPage" runat="server" Text=""></asp:Label>
总页数:<asp:Label ID="lblEndPage" runat="server" Text=""></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnQuery" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btndelete" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
②,改造下List.aspx.cs 代码:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace LinqDemo { public partial class List : System.Web.UI.Page { //页数 public int pagesize = 5; //当前页 public int pageindex = 0; protected void Page_Load(object sender, EventArgs e) { btndelete.Attributes.Add("onclick", "javascript:return window.confirm('确定删除吗?')"); btnAdd.Attributes.Add("onclick", "javascript:return window.confirm('确定添加吗?')"); if (!IsPostBack) { Bind(); } } protected void btnQuery_Click(object sender, EventArgs e) { ViewState["pageindex"] = 0; Bind(); } public IQueryable<InStore> I_store { get { var predicate = PredicateBuilder.True<InStore>(); if (!string.IsNullOrEmpty(txt_proname.Text)) { predicate = predicate.And(p => p.ProName.Contains(txt_proname.Text)); } if (!string.IsNullOrEmpty(txt_insertcount.Text)) { predicate = predicate.And(p => p.InsertCount.Equals(txt_insertcount.Text)); } InStoreDataContext db = new InStoreDataContext(); if (ViewState["pageindex"] != null) { pageindex = Convert.ToInt32(ViewState["pageindex"]); } //总条数 lblAllCount.Text = db.InStore.Where(predicate).Count().ToString(); //当前页 lblCurrentPage.Text = (pageindex+1).ToString(); //总页数 lblEndPage.Text = (GetEndPage+1).ToString(); return db.InStore.Where(predicate).Skip(pageindex * pagesize).Take(pagesize); } } /// <summary> /// 最后一页 /// </summary> public int GetEndPage { get { if (Convert.ToInt32(lblAllCount.Text) % pagesize == 0) { return Convert.ToInt32(lblAllCount.Text) / pagesize-1; } else { return Convert.ToInt32(lblAllCount.Text) / pagesize ; } } } /// <summary> /// 判断按钮页的显示和隐藏 /// </summary> public void SetLbEnable() { if (lblCurrentPage.Text == "1") { lbFirstPage.Enabled = false; lbFrontPage.Enabled = false; } else { lbFirstPage.Enabled = true; lbFrontPage.Enabled = true; } if (lblCurrentPage.Text == lblEndPage.Text) { lbEndPage.Enabled = false; lbNextPage.Enabled = false; } else { lbEndPage.Enabled = true; lbNextPage.Enabled = true; } } /// <summary> /// 查询函数 /// </summary> private void Bind() { GridView1.DataSource = I_store; GridView1.DataBind(); SetLbEnable(); } /// <summary> /// 添加函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAdd_Click(object sender, EventArgs e) { InStoreDataContext db = new InStoreDataContext(); InStore store = new InStore(); store.Id = int.Parse(txt_id.Text); store.InsertCount = int.Parse(txt_insertcount.Text); store.InsertDay = DateTime.Now; store.ProName = txt_proname.Text; db.InStore.InsertOnSubmit(store); db.SubmitChanges(); txt_insertcount.Text = ""; txt_proname.Text = ""; txt_id.Text = ""; Bind(); } /// <summary> /// 删除函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btndelete_Click(object sender, EventArgs e) { InStoreDataContext db = new InStoreDataContext(); for (int i = 0; i < GridView1.Rows.Count; i++) { HtmlInputCheckBox chk = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("chk"); if (chk.Checked) { InStore store = db.InStore.First(p => p.Id.Equals(int.Parse(chk.Value))); db.InStore.DeleteOnSubmit(store); db.SubmitChanges(); } } Bind(); } /// <summary> /// 修改函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpdate_Click(object sender, EventArgs e) { InStoreDataContext db = new InStoreDataContext(); for (int i = 0; i < GridView1.Rows.Count; i++) { HtmlInputCheckBox chk = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("chk"); if (chk.Checked) { InStore store = db.InStore.First(p => p.Id.Equals(int.Parse(chk.Value))); Response.Redirect("edit.aspx?id=" + chk.Value + ""); } } } /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbFirstPage_Click(object sender, EventArgs e) { ViewState["pageindex"] = 0; Bind(); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbNextPage_Click(object sender, EventArgs e) { ViewState["pageindex"] = Convert.ToInt32(ViewState["pageindex"]) + 1; Bind(); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbFrontPage_Click(object sender, EventArgs e) { ViewState["pageindex"] = Convert.ToInt32(ViewState["pageindex"]) - 1; Bind(); } /// <summary> /// 最后一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbEndPage_Click(object sender, EventArgs e) { ViewState["pageindex"] = GetEndPage; Bind(); } //全选 protected void chkall_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chkall = (CheckBox)GridView1.HeaderRow.FindControl("chkall"); HtmlInputCheckBox chk = (HtmlInputCheckBox)GridView1.Rows[i].FindControl("chk"); if (chkall.Checked == true) { chk.Checked = true; } else { chk.Checked = false; } } } } }
运行后效果图如下:
备注:LINQ的分页功能,主要是两个属性,Skip,代表的是从第几条数据开始算起,Take代表的是每页的产生的条数。
例如:
db.InStore.Where(predicate).Skip(0).Take(5); 代表的是从第1条数据开始,产生5条数据,
db.InStore.Where(predicate).Skip(5).Take(5); 代表的是从第6条数据开始,产生5条数据。
- linq to sql 学习(6)linq的分页实例
- linq to sql 学习(8)linq的多表查询实例
- Linq to sql 分页
- Linq to sql 分页
- LINQ to SQL实例
- LINQ to SQL实例
- linq to sql 的学习
- Linq to sql之简单的分页
- linq to sql 学习(4)增删改查实例
- LINQ to SQL 学习
- Linq to SQL学习
- LINQ to SQL 学习
- LinQ to Sql 分页,增,删,改 实例
- (LINQ 学习系列)(2)LINQ to SQL 查询
- linq,linq to sql
- 什么是Linq to sql 学习linq 1
- 小白的LINQ TO SQL学习
- (LINQ 学习系列)(1)使用 LINQ to SQL 的典型步骤
- strcpy和memcpy
- linq to sql 学习(5)关于排序的介绍
- Message Reflection for Windows Controls
- 优秀的工作流引擎的特点144个
- 主元分析(PCA)理论分析及应用
- linq to sql 学习(6)linq的分页实例
- 影响时间需求分析
- 关于主线同分支合并的概念及如何使用的误区
- VC++中使用Disassembly查看代码
- Windows XP + Apache 2.2.4 + PHP 5.2 + MySQL 5.0.18 +eclipse3.2+wampp-2.2.zip+HardCore环境配置说明
- SQL2005新手开发常见问题
- HSUPA技术实现及其应用分析
- 拍卖行竞拍存储过程,存在什么问题呢?
- PKU1664 放苹果