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 %>'>
后台:
 protected string FormatDate(object date) {
protected string FormatDate(object date) {
 if (date == DBNull.Value){
if (date == DBNull.Value){
 return "n/a";
    return "n/a";
 }
}
 try{
try{
 return ((DateTime)date).ToShortDateString();
return ((DateTime)date).ToShortDateString();
 }catch{
  }catch{
 return "n/a";
   return "n/a";
 }
  }
 }
  }
 protected string FormatMoney(object amount) {
  protected string FormatMoney(object amount) {
 if (amount == DBNull.Value){
   if (amount == DBNull.Value){
 return String.Format("{0:C}", 0);
    return String.Format("{0:C}", 0);
 }
  }
 return String.Format("{0:C}", amount);
  return String.Format("{0:C}", amount);
 }
 }
 protected void itemDataBoundRepeater_ItemDataBound(object source,
  protected void itemDataBoundRepeater_ItemDataBound(object source, 
 RepeaterItemEventArgs e) {
                                        RepeaterItemEventArgs e) {
 if (e.Item.ItemType == ListItemType.AlternatingItem ||
   if (e.Item.ItemType == ListItemType.AlternatingItem || 
 e.Item.ItemType == ListItemType.Item){
                               e.Item.ItemType == ListItemType.Item){
 Literal lit = (Literal)e.Item.FindControl("see");
    Literal lit = (Literal)e.Item.FindControl("see");
 if (lit != null){
    if (lit != null){
 Owner owner = (Owner)e.Item.DataItem;
     Owner owner = (Owner)e.Item.DataItem;
 if (owner.Pets.Count == 0){
     if (owner.Pets.Count == 0){
 lit.Text = "no pets";
      lit.Text = "no pets";
 }else{
     }else{
 lit.Text = "see pets";
      lit.Text = "see pets";
 }
     }
 }
    }
 }
   }
 }
 }
Nested Data
 ds.Relations.Add(new DataRelation("CustomerOrders",
ds.Relations.Add(new DataRelation("CustomerOrders", 
 ds.Tables[0].Columns["CustomerId"],
          ds.Tables[0].Columns["CustomerId"], 
 ds.Tables[1].Columns["CustomerId"]));
DataSource='<%# ((DataRowView)Container.DataItem).CreateChildView("CustomerOrders")%>'
          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")%>'
 
 
DataItem:如果你用的数据源是Table、DataSet之类的话,那么它可以强制类型转换为DataRowView;如果你用的是自定义实体集合,那么它可以强制类型转换为相应的实体。
<%# ((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 %>'>
后台:
 protected string FormatDate(object date) {
protected string FormatDate(object date) { if (date == DBNull.Value){
if (date == DBNull.Value){ return "n/a";
    return "n/a"; }
} try{
try{ return ((DateTime)date).ToShortDateString();
return ((DateTime)date).ToShortDateString(); }catch{
  }catch{ return "n/a";
   return "n/a"; }
  } }
  } protected string FormatMoney(object amount) {
  protected string FormatMoney(object amount) { if (amount == DBNull.Value){
   if (amount == DBNull.Value){ return String.Format("{0:C}", 0);
    return String.Format("{0:C}", 0); }
  } return String.Format("{0:C}", amount);
  return String.Format("{0:C}", amount); }
 } protected void itemDataBoundRepeater_ItemDataBound(object source,
  protected void itemDataBoundRepeater_ItemDataBound(object source,  RepeaterItemEventArgs e) {
                                        RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem ||
   if (e.Item.ItemType == ListItemType.AlternatingItem ||  e.Item.ItemType == ListItemType.Item){
                               e.Item.ItemType == ListItemType.Item){ Literal lit = (Literal)e.Item.FindControl("see");
    Literal lit = (Literal)e.Item.FindControl("see"); if (lit != null){
    if (lit != null){ Owner owner = (Owner)e.Item.DataItem;
     Owner owner = (Owner)e.Item.DataItem; if (owner.Pets.Count == 0){
     if (owner.Pets.Count == 0){ lit.Text = "no pets";
      lit.Text = "no pets"; }else{
     }else{ lit.Text = "see pets";
      lit.Text = "see pets"; }
     } }
    } }
   } }
 }Nested Data
 ds.Relations.Add(new DataRelation("CustomerOrders",
ds.Relations.Add(new DataRelation("CustomerOrders",  ds.Tables[0].Columns["CustomerId"],
          ds.Tables[0].Columns["CustomerId"],  ds.Tables[1].Columns["CustomerId"]));
          ds.Tables[1].Columns["CustomerId"]));DataSource='<%# DataBinder.Eval(Container.DataItem, "CutomerOrders")%>'
DataSource="<%# ((Owner)Container.DataItem).Pets%>"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Pets")%>'


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

3
 RepeaterCommandEventArgs e) {
                                    RepeaterCommandEventArgs e) {4
 int customerId = Convert.ToInt32(e.CommandArgument);
   int customerId = Convert.ToInt32(e.CommandArgument);5
 switch (e.CommandName.ToUpper()){
   switch (e.CommandName.ToUpper()){6
 case "DELETE":
    case "DELETE":7
 CustomerUtility.DeleteCustomer(customerId);
     CustomerUtility.DeleteCustomer(customerId);8
 BindEventRepeater(false);
     BindEventRepeater(false);9
 break;
     break;10
 case "Add":
    case "Add":11
 //doesn't actually do antyhing right now.
     //doesn't actually do antyhing right now.12
 break;
     break;13
 }
   }14
 }
  }DataItem:如果你用的数据源是Table、DataSet之类的话,那么它可以强制类型转换为DataRowView;如果你用的是自定义实体集合,那么它可以强制类型转换为相应的实体。
.jpg) 
  
 
                    
                     
                    
                 
                    
                
 


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号