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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </h1>
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 主界面</h1>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" Text="代号:"></asp:Label>
            <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
&nbsp;
            <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
&nbsp;
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="查询" />
        &nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加数据" />
    
        </p>
        <p>
            &nbsp;</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>&nbsp;<a href="Delete.aspx?code=<%#Eval("Code") %>">删除</a></td>
                  </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
    </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");
    }
}

 

posted @ 2015-12-24 22:34  m-n  阅读(160)  评论(0编辑  收藏  举报