对于MS的数据绑定方法的理解
2008-05-30 20:36
393 查看
深入了解了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)
在这个方法中有一个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>
不妨大家打开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)
在这个方法中有一个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>
相关文章推荐
- 关于c#中双主键表数据如何绑定到DataTable的方法(针对于dataset.xsd的特殊情况)
- 关于c#中双主键表数据如何绑定到DataTable的方法(针对于dataset.xsd的特殊情况)
- 理解$watch ,$apply 和 $digest --- 理解数据绑定过程
- WPF——数据绑定(二)绑定方法—绑定本地对象
- WCF 使用 Https 绑定数据(包括分布式WCF事务配置方法)
- 张军-大数据的理解与分布式进化计算方法
- WinForm中comboBox控件数据绑定实现方法
- MySQL对于有大量重复数据表的处理方法
- 对于java中的native方法的一些理解
- Android DataBinding介绍(一)——简介、数据及方法事件绑定
- GridView使用DataBind方法绑定数据时的分页问题[转]
- 对于Hibernate的openSession方法为什么不是线程安全的源码理解
- ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)
- datalist 的用法。也是增删改查,但是比较智能。用数据绑定的方式,可以有不同的显示方法,下面是对一个表的增删改查的参考代码
- Jquery中设置模板绑定数据的方法
- vue中属性动态绑定数据方法
- JavaScript 进阶之深入理解数据双向绑定
- JMeter中对于Json数据的处理方法
- 理解Angular数据双向绑定
- ASPxNavBar 代码绑定数据的方法