20151221:Web复习:主界面
前台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> <style type="text/css"> * { margin: 0px auto; padding: 0px; } </style> </head> <body> <form id="form1" runat="server"> <div id="aa" style="width:1000px"> <h1> </h1> <h1> 主界面</h1> <p> </p> <p> </p> <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="查询" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加数据" /> </p> <p> </p> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table width="800" border="0" cellspacing="1" cellpadding="1" bgcolor="#6600FF"> <tr> <td width="120" height="30" align="center" valign="middle" bgcolor="#FFFFFF">代号</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">姓名</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">性别</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF" >民族</td> <td width="200" align="center" valign="middle" bgcolor="#FFFFFF">生日</td> <td width="200" align="center" valign="middle" bgcolor="#FFFFFF">操作</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td height="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> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <br /> </div> </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) { 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(); List<Info> list = 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"); } }