Container.DataItem. What exactly is this?
来源:互联网 发布:js中用十进制表示字符 编辑:程序博客网 时间:2024/05/18 23:12
Container.DataItem. What exactly is this?
Recently I had to bind to an array of strings and I wasn't sure how to show the value of the current item. After a little bit of help from the DevTheo and a little bit of thinking I figured out the solution was very simple. All i had to do was bind it like this:
<%# Container.DataItem %>
So what is this expression exactly? The <%# %> means this is a DataBinding expression and Container.DataItem is an alias for the current item in the datasource. In other words, if you are binding to a collection of objects Container.DataItem is the current row of that collection.
If we use a DataView (default type when using SQLDatasource) to bind then Container.DataItem is a DataRowView. For a collection of objects of type Animals the type will be Animal and for an array of strings Container.DataItem will be a string.
Since .NET doesn't know to what type of collection object you will bind to DataItem will return an object. For C# you will have to cast your DataItem:
<%# ((DataRowView)Container.DataItem)["SomeProperty"] %>
There's another way of achieving the same result and that is using DataBinder.Eval.
<%# DataBinder.Eval(Container.DataItem, "SomeProperty")%>
DataBinder.Eval is a helper method that uses reflection to find the type of the DataItem in order to display the data correctly. The benefit of the Eval method is that if your type changes in the future you don't need to change the code, but in the other hand reflection causes performance to slow down.
Happy Programming!
- Container.DataItem. What exactly is this?
- What exactly is read_rnd_buffer_size
- What exactly is "iowait"?
- What exactly is "iowait"?
- What Exactly Is the Error?
- What is a JavaBean exactly?
- What Exactly Is A PLL?(ZT)
- What exactly is a Neural Network?
- What exactly is null in Java?[转]
- What, exactly, is a Product Manager?
- What's the deal with Databinder.Eval and Container.DataItem?
- What is this site?
- what is this?
- what is this api
- What is this?
- 空指针NULL 和 What exactly is nullptr?
- What is the probability that you hire exactly twice?
- Container.DataItem的含义
- 25岁后要知道的事
- 信息立方体 infocube
- 校验MD5值是什么意思?
- VC中判断窗体是否存在
- 离职心情
- Container.DataItem. What exactly is this?
- dom4j学习笔记(二)
- 利用Apache的FileUpload类库实现文件上传
- 10款你应该了解的多媒体相关的Adobe AIR应用
- MyEclipse 6.5GA 最新下载、注册、汉化(2008年11月更新)
- 学习document.getElementById()的使用
- C++ windows下编写串口程序的步骤
- 很美很美的爱情哲学
- 给Ubuntu 8.10 的 3D桌面不同桌面设置不同壁纸的方法