Repeater绑定对象

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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 >

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
后台代码
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();
}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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();
}
}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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" > &nbsp; </ th >
< th scope ="col" > Name </ th >
< th align ="right" scope ="col" > Price </ th >
< th scope ="col" > &nbsp; </ 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 >

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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: 在此处添加构造函数逻辑
//
}

}

 

转载于:https://www.cnblogs.com/qhnokia/archive/2009/09/25/1574114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值