Mastering DataBinding In Asp.net

<%# DataBinder.Eval(Container.DataItem, "customerId") %>
<%# ((DataRowView)Container.DataItem)["customerId"] %>
<%# ((User)Container.DataItem).UserName%>
<%# FormatDate(DataBinder.Eval(Container.DataItem, "Ordered"))%>
<%# FormatMoney(DataBinder.Eval(Container.DataItem, "Amount"))%>
Visible='<%# (int)DataBinder.Eval(Container.DataItem, "Pets.Count") > 0 %>'>

后台:
Mastering DataBinding In Asp.net protected   string  FormatDate( object  date)  {
Mastering DataBinding In Asp.net
if (date == DBNull.Value){
Mastering DataBinding In Asp.net    
return "n/a";
Mastering DataBinding In Asp.net}

Mastering DataBinding In Asp.net
try{
Mastering DataBinding In Asp.net
return ((DateTime)date).ToShortDateString();
Mastering DataBinding In Asp.net  }
catch{
Mastering DataBinding In Asp.net   
return "n/a";
Mastering DataBinding In Asp.net  }

Mastering DataBinding In Asp.net  }

Mastering DataBinding In Asp.net  
protected   string  FormatMoney( object  amount)  {
Mastering DataBinding In Asp.net   
if (amount == DBNull.Value){
Mastering DataBinding In Asp.net    
return String.Format("{0:C}"0);
Mastering DataBinding In Asp.net  }

Mastering DataBinding In Asp.net  
return String.Format("{0:C}", amount);
Mastering DataBinding In Asp.net }

Mastering DataBinding In Asp.net    protected   void  itemDataBoundRepeater_ItemDataBound( object  source, 
Mastering DataBinding In Asp.net                                        RepeaterItemEventArgs e) 
{
Mastering DataBinding In Asp.net   
if (e.Item.ItemType == ListItemType.AlternatingItem || 
Mastering DataBinding In Asp.net                               e.Item.ItemType 
== ListItemType.Item){
Mastering DataBinding In Asp.net    Literal lit 
= (Literal)e.Item.FindControl("see");
Mastering DataBinding In Asp.net    
if (lit != null){
Mastering DataBinding In Asp.net     Owner owner 
= (Owner)e.Item.DataItem;
Mastering DataBinding In Asp.net     
if (owner.Pets.Count == 0){
Mastering DataBinding In Asp.net      lit.Text 
= "no pets";
Mastering DataBinding In Asp.net     }
else{
Mastering DataBinding In Asp.net      lit.Text 
= "see pets";
Mastering DataBinding In Asp.net     }

Mastering DataBinding In Asp.net    }

Mastering DataBinding In Asp.net   }

Mastering DataBinding In Asp.net }

Nested Data
Mastering DataBinding In Asp.net ds.Relations.Add( new  DataRelation( " CustomerOrders "
Mastering DataBinding In Asp.net          ds.Tables[
0 ].Columns[ " CustomerId " ], 
Mastering DataBinding In Asp.net          ds.Tables[
1 ].Columns[ " CustomerId " ]));
DataSource='<%# ((DataRowView)Container.DataItem).CreateChildView("CustomerOrders")%>'
DataSource='<%# DataBinder.Eval(Container.DataItem, "CutomerOrders")%>'

DataSource="<%# ((Owner)Container.DataItem).Pets%>"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Pets")%>'

Mastering DataBinding In Asp.net

Mastering DataBinding In Asp.net
 1 Mastering DataBinding In Asp.net    protected   void  eventRepeater_ItemCommand( object  s, 
 2 Mastering DataBinding In Asp.net
 3 Mastering DataBinding In Asp.net                                    RepeaterCommandEventArgs e)  {
 4Mastering DataBinding In Asp.net   int customerId = Convert.ToInt32(e.CommandArgument);
 5Mastering DataBinding In Asp.net   switch (e.CommandName.ToUpper()){
 6Mastering DataBinding In Asp.net    case "DELETE":
 7Mastering DataBinding In Asp.net     CustomerUtility.DeleteCustomer(customerId);
 8Mastering DataBinding In Asp.net     BindEventRepeater(false);
 9Mastering DataBinding In Asp.net     break;
10Mastering DataBinding In Asp.net    case "Add":
11Mastering DataBinding In Asp.net     //doesn't actually do antyhing right now.
12Mastering DataBinding In Asp.net     break;
13Mastering DataBinding In Asp.net   }

14Mastering DataBinding In Asp.net  }


DataItem:如果你用的数据源是Table、DataSet之类的话,那么它可以强制类型转换为DataRowView;如果你用的是自定义实体集合,那么它可以强制类型转换为相应的实体。

你可能感兴趣的:(asp.net)