代码
Code
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " 绑定对象.aspx.cs " Inherits = " 绑定对象 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title ></ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Repeater ID ="Repeater1" runat ="server" >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem, " Name " ) %> </ br >
</ ItemTemplate >
</ asp:Repeater >
</ div >
</ form >
</ body >
</ html >
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " 绑定对象.aspx.cs " Inherits = " 绑定对象 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title ></ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Repeater ID ="Repeater1" runat ="server" >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem, " Name " ) %> </ br >
</ ItemTemplate >
</ asp:Repeater >
</ div >
</ form >
</ body >
</ html >
代码
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 绑定对象 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Person Tom = new Person( 1 , " Tom " );
Person Jack = new Person( 2 , " Jack " );
List < Person > persons = new List < Person > ();
persons.Add(Tom);
persons.Add(Jack);
Repeater1.DataSource = persons;
Repeater1.DataBind();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 绑定对象 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Person Tom = new Person( 1 , " Tom " );
Person Jack = new Person( 2 , " Jack " );
List < Person > persons = new List < Person > ();
persons.Add(Tom);
persons.Add(Jack);
Repeater1.DataSource = persons;
Repeater1.DataBind();
}
}
代码
WishListControl.ascx.cs
using System;
using System.Web.UI.WebControls;
using PetShop.BLL;
using PetShop.Model;
using System.Collections.Generic;
namespace PetShop.Web {
public partial class WishListControl : System.Web.UI.UserControl {
/// <summary>
/// Handle Page load event
/// </summary>
protected void Page_PreRender( object sender, EventArgs e) {
if ( ! IsPostBack) {
BindCart();
}
}
/// <summary>
/// Bind repeater to Cart object in Profile
/// </summary>
private void BindCart() {
ICollection < CartItemInfo > wishList = Profile.WishList.CartItems;
if (wishList.Count > 0 ) {
repWishList.DataSource = wishList;
repWishList.DataBind();
}
else {
repWishList.Visible = false ;
lblMsg.Text = " Your wish list is empty. " ;
}
}
/// <summary>
/// Handler for Delete/Move buttons
/// </summary>
protected void CartItem_Command( object sender, CommandEventArgs e) {
switch (e.CommandName.ToString()) {
case " Del " :
Profile.WishList.Remove(e.CommandArgument.ToString());
break ;
case " Move " :
Profile.WishList.Remove(e.CommandArgument.ToString());
Profile.ShoppingCart.Add(e.CommandArgument.ToString());
break ;
}
Profile.Save();
BindCart();
}
}
}
using System;
using System.Web.UI.WebControls;
using PetShop.BLL;
using PetShop.Model;
using System.Collections.Generic;
namespace PetShop.Web {
public partial class WishListControl : System.Web.UI.UserControl {
/// <summary>
/// Handle Page load event
/// </summary>
protected void Page_PreRender( object sender, EventArgs e) {
if ( ! IsPostBack) {
BindCart();
}
}
/// <summary>
/// Bind repeater to Cart object in Profile
/// </summary>
private void BindCart() {
ICollection < CartItemInfo > wishList = Profile.WishList.CartItems;
if (wishList.Count > 0 ) {
repWishList.DataSource = wishList;
repWishList.DataBind();
}
else {
repWishList.Visible = false ;
lblMsg.Text = " Your wish list is empty. " ;
}
}
/// <summary>
/// Handler for Delete/Move buttons
/// </summary>
protected void CartItem_Command( object sender, CommandEventArgs e) {
switch (e.CommandName.ToString()) {
case " Del " :
Profile.WishList.Remove(e.CommandArgument.ToString());
break ;
case " Move " :
Profile.WishList.Remove(e.CommandArgument.ToString());
Profile.ShoppingCart.Add(e.CommandArgument.ToString());
break ;
}
Profile.Save();
BindCart();
}
}
}
代码
WishListControl。ascx
<% @ Control Language = " C# " AutoEventWireup = " true " CodeFile = " WishListControl.ascx.cs " Inherits = " PetShop.Web.WishListControl " %>
< div align ="left" class ="cartHeader" > Items in your Wish List </ div >
< asp:Label runat ="server" ID ="lblMsg" EnableViewState ="false" CssClass ="label" />
< asp:Repeater ID ="repWishList" runat ="server" >
< HeaderTemplate >
< table cellspacing ="0" cellpadding ="3" rules ="all" border ="0" class ="cart" align ="center" width ="387" >
< tr class ="labelLists" >
< th scope ="col" > </ th >
< th scope ="col" > Name </ th >
< th align ="right" scope ="col" > Price </ th >
< th scope ="col" > </ th >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr class ="listItem" >
< td >
< asp:ImageButton ID ="btnDelete" runat ="server" AlternateText ="Delete" CausesValidation ="false"
CommandArgument ='<%# Eval("ItemId") % > ' CommandName="Del" ImageUrl="~/Comm_Images/button-delete.gif"
OnCommand="CartItem_Command" ToolTip="Delete" />
</ td >
< td style ="width:100%;" >
< a runat ="server" href ='<%# string.Format("~/Items.aspx?itemId ={0}&productId={1}&categoryId={2}", Eval("ItemId"), Eval("ProductId"), Eval("CategoryId")) % > '> <% # string .Format( " {0} {1} " , Eval ( " Name " ), Eval ( " Type " )) %> </ a >
</ td >
< td align ="right" > <% # Eval ( " Price " , " {0:c} " ) %> </ td >< td >
< asp:ImageButton ID ="btnToWishList" runat ="server" AlternateText ="Move to cart" CausesValidation ="false"
CommandArgument ='<%# Eval("ItemId") % > ' CommandName="Move" ImageUrl="~/Comm_Images/button-cart.gif"
OnCommand="CartItem_Command" ToolTip="Move to cart" />
</ td >
</ tr >
</ ItemTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >
</ asp:Repeater >
<% @ Control Language = " C# " AutoEventWireup = " true " CodeFile = " WishListControl.ascx.cs " Inherits = " PetShop.Web.WishListControl " %>
< div align ="left" class ="cartHeader" > Items in your Wish List </ div >
< asp:Label runat ="server" ID ="lblMsg" EnableViewState ="false" CssClass ="label" />
< asp:Repeater ID ="repWishList" runat ="server" >
< HeaderTemplate >
< table cellspacing ="0" cellpadding ="3" rules ="all" border ="0" class ="cart" align ="center" width ="387" >
< tr class ="labelLists" >
< th scope ="col" > </ th >
< th scope ="col" > Name </ th >
< th align ="right" scope ="col" > Price </ th >
< th scope ="col" > </ th >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr class ="listItem" >
< td >
< asp:ImageButton ID ="btnDelete" runat ="server" AlternateText ="Delete" CausesValidation ="false"
CommandArgument ='<%# Eval("ItemId") % > ' CommandName="Del" ImageUrl="~/Comm_Images/button-delete.gif"
OnCommand="CartItem_Command" ToolTip="Delete" />
</ td >
< td style ="width:100%;" >
< a runat ="server" href ='<%# string.Format("~/Items.aspx?itemId ={0}&productId={1}&categoryId={2}", Eval("ItemId"), Eval("ProductId"), Eval("CategoryId")) % > '> <% # string .Format( " {0} {1} " , Eval ( " Name " ), Eval ( " Type " )) %> </ a >
</ td >
< td align ="right" > <% # Eval ( " Price " , " {0:c} " ) %> </ td >< td >
< asp:ImageButton ID ="btnToWishList" runat ="server" AlternateText ="Move to cart" CausesValidation ="false"
CommandArgument ='<%# Eval("ItemId") % > ' CommandName="Move" ImageUrl="~/Comm_Images/button-cart.gif"
OnCommand="CartItem_Command" ToolTip="Move to cart" />
</ td >
</ tr >
</ ItemTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >
</ asp:Repeater >
代码
Person类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public Person()
{ }
public Person( int id, string name)
{
this .ID = id;
this .Name = name;
//
// TODO: 在此处添加构造函数逻辑
//
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public Person()
{ }
public Person( int id, string name)
{
this .ID = id;
this .Name = name;
//
// TODO: 在此处添加构造函数逻辑
//
}
}