Repeater 列表绑定数据源显示以及增删改查

主界面aspx里的代码,很重要哦

<%@ 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>

    <style>

        .tr1 {

            background-color:#f00;

            text-align:center;

        }

  .tr2

        {

      text-align:center;

        }



    </style>

</head>

<body>

    <p>

        <br />

    </p>

    <form id="form1" runat="server">

    <div>

    

        <asp:Repeater ID="Repeater1" runat="server">



<HeaderTemplate> <center> <table width="70%" border="1"><tr class="tr1"><td>编号</td><td>名称</td><td>价格</td><td>操作</td></tr> </HeaderTemplate> <ItemTemplate> <tr bgcolor="#FFFFCC" class="tr2"><td><%#Eval("Code") %></td><td><%#Eval("Name") %></td><td><%#Eval("Price") %></td><td><a href="Update.aspx?code=<%#Eval("Code") %>">修改</a><a href="Delete.aspx?code=<%#Eval("Code") %>"> 删除</a></td></tr> </ItemTemplate> <AlternatingItemTemplate> <tr class="tr2"><td><%#Eval("Code") %></td><td><%#Eval("Name") %></td><td><%#Eval("Price") %></td><td><a href="Update.aspx?code=<%#Eval("Code") %>">修改</a><a href="Delete.aspx?code=<%#Eval("Code") %>"> 删除</a></td></tr> </AlternatingItemTemplate> <FooterTemplate> </table></center> </FooterTemplate> </asp:Repeater> <a href="Insert.aspx"> 添加</a> </div> </form> </body> </html>


aspx里的后台代码

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {





        DataClassesDataContext _Context = new DataClassesDataContext();

        //给Repeater绑定数据源

        Repeater1.DataSource = _Context.Car.ToList();

        Repeater1.DataBind();

    }

}

 

你可能感兴趣的:(增删改查)