对于MS的数据绑定方法的理解

深入了解了MS的DataBinder.Eval()方法,到网上也找了很多资料,这方法确实不怎么好,看样子MS还是有待提高,

不妨大家打开MS的代码来看看他是这么实现这个数据邦定的,这样一切都清楚了。DataBinder是System.Web.UI中的一个静态类。首先看一下它的Eval方法:
public   static   object  Eval ( object  container,  string  expression)
{
     
if  (expression  ==   null )
     {
          
throw   new  ArgumentNullException( " expression " );
     }
     expression 
=  expression.Trim();
     
if  (expression.Length  ==   0 )
     {
          
throw   new  ArgumentNullException( " expression " );
     }
     
if  (container  ==   null )
     {
          
return   null ;
     }
     
return  DataBinder.Eval(container, expression.Split(DataBinder.expressionPartSeparator));
}

这个方法调用了另外一个重载的Eval方法我们接着看这个方法:

private   static   object  Eval ( object  container,  string [] expressionParts)
... {
     
object obj1 = container;
     
for (int num1 = 0;(num1 < expressionParts.Length) && (obj1 != null); num1++)
     
...{
          
string text1 = expressionParts[num1];
          
if (text1.IndexOfAny(DataBinder.indexExprStartChars) < 0)
          
...{
               obj1 
= DataBinder.GetPropertyValue(obj1, text1);
          }

          
else
          
...{
               obj1 
= DataBinder.GetIndexedPropertyValue(obj1, text1);
          }

     }

     
return obj1;
}

 在这个方法中有一个GetIndexedPropertyValue方法是处理索引用的,一般邦定用到的是上边的GetPropertyValue方法。继续看GetPropertyValue方法:

public   static   object  GetPropertyValue ( object  container,  string  propName)
{
     
if  (container  ==   null )
     {
          
throw   new  ArgumentNullException( " container " );
     }
     
if  ( string .IsNullOrEmpty(propName))
     {
          
throw   new  ArgumentNullException( " propName " );
     }
     
object  obj1  =   null ;
     PropertyDescriptor descriptor1 
=  TypeDescriptor.GetProperties(container).Find(propName,  true );
     
if  (descriptor1  ==   null )
     {
          
throw   new  HttpException(SR.GetString( " DataBinder_Prop_Not_Found " new   object []{container.GetType().FullName, propName}));
     }
     
return  descriptor1.GetValue(container);
}
 

 晕~~ 这个方法中利用TypeDescriptor的GetProperties方法将邦定对象反射然后用GetValue获取我们想要的值!  看到这里我想大家都已经看清楚了,原来当我们调用<%#DataBinder.Eval(Container.DataItem,"xxx") %>时是将Container.DataItem反射然后用GetValue取值的,也就是说如果你页面中有多个DataBinder的话每个都要反射、取值!晕倒~ 反射是非常消耗资源而且效率低下的这个不用我说了。
     解决的办法是什么?  当然就是不用撒,我原来在网上找了写资料, 决定不用Eval而改用自己的绑定方法,

办法一:
如绑定购物车数据时,这样写的
1).导入实体类命名空间
<%@ Import Namespace="Plugins.ProductSystem.Domain" %>

2).Repeater里数据绑定方法
<a href="/Product/ProductDetail.aspx?id=<%# ((Product)Container.DataItem).Id %>" target="_blank"><%# Xnet.Common.Text.String.Left(((Product)Container.DataItem).ProName,50) %></a>
显然这样只是用到了实体类对象 ((Product)Container.DataItem) 而已,而不要用那效率低的DataBinder.Eval()了,只是这样显得绑定代码比较长,比较混乱是吧,
     现在又找到了个好办法,和大家分享下

办法二:
我们可以在DataList、Repeater的ItemCreated事件中做文章,我们在定义一个protected 变量,变量类型为邦定数据类型。
如绑定DataTable数据集的protected DataRowView Info =null; 不过现在一般都用Ilist 或 Iist<>代替了
然后再ItemCreated中进行转换付值。我习惯了用范型了,本例用序列化保存Ilist<Product>购物车信息     
如: 
        protected Product product = null;
        protected void rShoppingCart_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            product = e.Item.DataItem as Product;
        }

则:Repeater里数据绑定方法有了很大的改善
<a href="/Product/ProductDetail.aspx?id=<%# product.Id %>" target="_blank"><%# Xnet.Common.Text.String.Left(product.ProName,50) %></a>


 

转载于:https://www.cnblogs.com/di305449473/archive/2008/05/30/1210939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值