前台HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %> <!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"> <div> <h1>主界面</h1> <p> <asp:Label ID="Label1" runat="server" Text="代号:"></asp:Label> <asp:TextBox ID="txtCode" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="查询" /> </p> </div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#6633CC"> <tr> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">代号</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">姓名</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">性别</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">民族</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">生日</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">操作</td> </tr> </HeaderTemplate> <FooterTemplate> </table> </FooterTemplate> <ItemTemplate> <tr> <td heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Code") %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Name") %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowSex() %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowNation() %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowBirthday() %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><a href="Update.aspx?code=<%#Eval("Code") %>">修改</a> <a href="Delete.aspx?code=<%#Eval("Code") %>">删除</a></td> </tr> </ItemTemplate> </asp:Repeater> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" /> </form> </body> </html>
后台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) { testDataContext context = new testDataContext(); //指定数据源 Repeater1.DataSource = context.Info; Repeater1.DataBind(); } } else { Response.Redirect("DengLu.aspx"); } } //性别的函数 点用函数显示男女 public string ShowSex() { return Convert.ToBoolean(Eval("Sex")) ? "男" : "女"; } public string ShowNation() { testDataContext context = new testDataContext(); //取每一项的Nation代号 是string类型的 放在code里面存着 string code = Eval("Nation").ToString(); //根据code查Nation表 只要他的Name 直接返回return return context.Nation.Where(p => p.Code == code).First().Name; } public string ShowBirthday() { //直接取birthda这一列 返回一个日期时间型 return Convert.ToDateTime(Eval("Birthday")).ToString("yyyy年MM月dd日"); } protected void Button2_Click(object sender, EventArgs e) { //组合查询 //第一步 取所有数据放在一个集合里面存着 testDataContext context = new testDataContext(); List<Info> list = context.Info.ToList(); //第二步 取第一个 和 第二个 查询条件 string code = txtCode.Text; if (code != "") { //从list集合里面筛选出Code等于输入的code list = list.Where(p => p.Code == code).ToList(); } string name = txtName.Text; if (name != "") { list = list.Where(p => p.Name.Contains(name)).ToList(); } //给Reapeater做数据源 Repeater1.DataSource = list; Repeater1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("Insert.aspx"); } }
网页显示图片:
时间: 2024-10-05 23:53:53