使用 linq 在网页上对用户信息增删改,组合查询,分页显示
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Users 的摘要说明 /// </summary> public partial class Users { public string SexStr { get { return Convert.ToBoolean(_Sex) ? "男" : "女"; } } public string BirStr { get { return Convert.ToDateTime(_Birthday).ToString("yyyy年MM月dd日"); } //DateTime? 型,带问号,最好转换一下 } public string Nationn { get { return Nation1.NationName; } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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"> <br /><br /> 用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 性别:<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Value="null" Text=""></asp:ListItem> <%--留下空白选项 全选--%> <asp:ListItem value="true" >男</asp:ListItem> <asp:ListItem value="false" >女</asp:ListItem> </asp:DropDownList> 民族:<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value ="null" Text=""></asp:ListItem> <%--留下空白选项 全选--%> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="查询" /> <br /><br /> <table style="width: 100%; text-align: center; background-color: navy"> <tr style="color: white"> <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: white"> <td><%#Eval("Ids") %></td> <td><%#Eval("Username") %></td> <td><%#Eval("Password") %></td> <td><%#Eval("Nickname") %></td> <td><%#Eval("SexStr") %></td> <td><%#Eval("BirStr") %></td> <td><%#Eval("Nationn") %></td> <td> 设置 删除 </td> </tr> </ItemTemplate> </asp:Repeater> <tr> <td style="color: white" > 共有 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 条记录 </td> </tr> </table> 第[<asp:Label ID="Label2" runat="server" Text="1"></asp:Label>]页 共[<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>]页 <asp:Button ID="But_up" runat="server" Text="上一页" /> <asp:Button ID="But_dow" runat="server" Text="下一页" /> </form> </body> </html>
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 { int pagelist = 3; //每页显示3条数据 protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; But_up.Click += But_up_Click; But_dow.Click += But_dow_Click; if (!IsPostBack) { using (Data0216_DataClassesDataContext con = new Data0216_DataClassesDataContext()) { //绑定民族 //DropDownList1.DataSource = con.Nation.ToList(); //DropDownList1.DataTextField = "NationName"; //DropDownList1.DataValueField = "NationCode"; 用此法绑定数据无法留空白选项, //DropDownList1.DataBind(); 空选项为此项全选 //绑定民族 foreach (Nation uu in con.Nation.ToList()) { ListItem li = new ListItem(); li.Text = uu.NationName; li.Value = uu.NationCode; DropDownList1.Items.Add(li); } //绑定所有记录 List<Users> ulist = con.Users.ToList(); Repeater1.DataSource = ulist.Take(pagelist); Repeater1.DataBind(); //共多少条记录 Label1.Text = ulist.Count.ToString(); //共多少页 Label3.Text = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(ulist.Count) / pagelist)).ToString(); } } } //下一页 void But_dow_Click(object sender, EventArgs e) { using (Data0216_DataClassesDataContext con = new Data0216_DataClassesDataContext()) { //获取下一页页数 int a = Convert.ToInt32(Label2.Text) +1; //调方法绑定数据 Repeater1.DataSource = select(con).Skip((a-1)*pagelist).Take(pagelist); Repeater1.DataBind(); //修改显示页数 Label2.Text = a.ToString(); } } //上一页 void But_up_Click(object sender, EventArgs e) { using (Data0216_DataClassesDataContext con = new Data0216_DataClassesDataContext()) { //获取下一页页数 int a = Convert.ToInt32(Label2.Text) - 1; //调方法绑定数据 Repeater1.DataSource = select(con).Skip((a-1)*pagelist).Take(pagelist); Repeater1.DataBind(); //修改显示页数 Label2.Text = a.ToString(); } } //组合查询 void Button1_Click(object sender, EventArgs e) { using (Data0216_DataClassesDataContext con = new Data0216_DataClassesDataContext()) { Repeater1.DataSource = select(con).Take(pagelist); ; Repeater1.DataBind(); } } //查询方法 public List<Users> select(Data0216_DataClassesDataContext con) { List<Users> ulist = con.Users.ToList(); //用户名 if (TextBox1.Text.Trim().Length < 0) { ulist = ulist.Where(r => r.Username.Contains(TextBox1.Text.Trim())).ToList(); } //性别 if (DropDownList2.SelectedValue != "null") { ulist = ulist.Where(r => r.Sex == Convert.ToBoolean(DropDownList2.SelectedValue)).ToList(); } //民族 if (DropDownList1.SelectedValue != "null") { ulist = ulist.Where(r => r.Nation == DropDownList1.SelectedValue).ToList(); } Label1.Text = ulist.Count.ToString();//共多少记录 //共多少页 Label3.Text = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(ulist.Count) / pagelist)).ToString(); return ulist; } }
-- 将组合查询写成一个方法,调用方法查询所有结果后,上下文对象关闭消失,
由于本案例中在绑定数据时存在扩展属性,所以需要再开一次数据库,需要再创建一个上下文对象,
所以在本案例中,查询方法需要传入一个上下文对象,在上一页、下一页中都要创建上下文对象,
-------------------------------------------------------