前台HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="_Main" %>
后台C#代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Main : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (Session["uid"] != null) { if (!IsPostBack) { TextDataContext context = new TextDataContext(); //指定数据源 Repeater1.DataSource = context.Info; Repeater1.DataBind(); } } else { Response.Redirect("Denglu.aspx"); } } public string ShowSex() { return Convert.ToBoolean( Eval("Sex")) ? "男" : "女"; } public string ShowNation() { TextDataContext context = new TextDataContext(); string code = Eval("Nation").ToString(); return context.Nation.Where(p => p.Code == code).First().Name; } public string ShowBirthday() { return Convert.ToDateTime(Eval("Birthday")).ToString("yyyy年MM月dd日"); } protected void Button2_Click(object sender, EventArgs e) { //组合查询 //取所有数据 TextDataContext context = new TextDataContext(); Listlist = context.Info.ToList();//造集合存放所有的数据 //取第一个查询条件 string code = txtCode.Text; if(code != "") { list = list.Where(p => p.Code == code).ToList(); } //取第二个查询条件 string name = txtName.Text; if (name != "") { list = list.Where(p => p.Name.Contains(name)).ToList(); } //给repeater做数据源 Repeater1.DataSource = list; Repeater1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { //添加 Response.Redirect("Insert.aspx"); }}