zoukankan      html  css  js  c++  java
  • 对于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>


     

  • 相关阅读:
    php tp6 踩坑(1)使用cookie助手函数 接口报500错误,但却没用错误信息问题
    php 杂项(2)--md5加密
    php tp6学习笔记(100) 正则表达式
    php 杂项(1)--单引号和双引号的区别
    pins和ports区别
    analyze&elaborate
    编译前后保存database
    DC层次化设计
    synthesis建立和保存
    DC综合过程和目标
  • 原文地址:https://www.cnblogs.com/di305449473/p/1210939.html
Copyright © 2011-2022 走看看