DataBinder.Eval方法研究

来源:互联网 发布:ei数据库检索 编辑:程序博客网 时间:2024/05/22 15:38

DataBinder.Eval方法研究

DataBinder.Eval方法:
用途:在运行时使用反射来分析和计算对象的数据绑定表达式
重载列表如下:
public static object Eval(object container,string expression);
public static object Eval(object container,string expression,string format);
假设后台代码如下:
IList list = new ArrayList();

Task task 
= new Task();

task.Title 
= "title1";

task.StartDate 
= DateTime.Now;

list.Add(task);

task 
= new Task();

task.Title 
= "title2";

task.StartDate 
= DateTime.Now.AddDays(1);

list.Add(task); 

task 
= new Task();

task.Title 
= "title3";

task.StartDate 
= DateTime.Now.AddDays(2);

list.Add(task); 

this.DataGrid1.DataSource = list;

this.DataGrid1.DataBind();


那么,常用的方式如下:
<asp:datagrid runat=server id=”DataGrid1”>

<Columns>

<asp:TemplateColumn HeaderText="开始日期">

  
<ItemTemplate>     
    
<%#DataBinder.Eval(Container.DataItem,"StartDate","{0,d}")%>
  
</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:datagrid>

分析:

1.第一个参数传进去的是Container.DataItem,那么看一下Container.DataItem是什么东西:
新增一列,如下:

<asp:TemplateColumn HeaderText="test">
<ItemTemplate>
<%#Container.DataItem%>
</ItemTemplate>
</asp:TemplateColumn>

运行结果如下图:

所以,此时的Container.DataItem是Task类型的对象,所以可以直接把Task的属性名作为DataBinder.Eval的第二个参数。
2.再看一下Container是什么东西:
把分析1中的代码的变成下面的样子:
<%#Container %>
运行结果如下图:

所以,这里的Container是一个DataGridItem的对象。接着看看Container和DataItem有什么关系:

DataGridItem类有一个属性就叫做DataItem,msdn中的解释为“获取或设置由 DataGrid 控件中的 DataGridItem 对象表示的数据项。”也就是说是数据源中的每一条,也就是列表list中的每一条(Task对象)。

所以,对于DataBinder.Eval(Container.DataItem,"StartDate"),参数Container.DataItem确定了将从某一个Task对象里取数据,StartDate指定了待取数据的访问路径。所以,这句话这么写也是OK的,DataBinder.Eval(Container," DataItem.StartDate"),即Container指定了将从一个DataGridItem里取数据,数据的访问路径是DataItem.StartDate。

 

在msdn上没有找到Container的解释,把<%#Container %>放到DataGrid的TemplateColumn里,得到的是DataGridItem,把<%#Container %>放到一个table下面,得到是ASP.TaskList_aspx,所以猜测通过Container,可以引用到当前控件所在的容器(解释的不够准确)

 

3.那么,DataBinder.Eval的第一个参数不用Container可不可以呢?是可以的:

<%#DataBinder.Eval(PageTitle,"Text")%>,假设PageTitle是一个Label,那么这句话就绑定到了PageTitle的Text属性。

结论:

说了这么多,对于所关心的如下结构的数据源怎样通过DataBinder.Eval来绑定呢?


假设数据源如下:

 

IList list = new ArrayList();

Task task 
= new Task();

object[] arr = new object[3];

task.Title 
= "title1";

task.StartDate 
= DateTime.Now;

arr[
0= "nihao";

arr[
1= task;

arr[
2= "wohao";

list.Add(arr); 

task 
= new Task();

task.Title 
= "title2";

task.StartDate 
= DateTime.Now.AddDays(1);

arr 
= new object[[3]];

arr[
0= "nihao";

arr[
1= task;

arr[
2= "wohao";

list.Add(arr);

task 
= new Task();

task.Title 
= "title3";

task.StartDate 
= DateTime.Now.AddDays(2);

arr 
= new object[3];

arr[
0= "nihao";

arr[
1= task;

arr[
2= "wohao";

list.Add(arr);

this.DataGrid1.DataSource = list;

this.Page.DataBind();


那么可以用下面的方法绑定到StartDate

<%#DataBinder.Eval(Container,"DataItem.[1].StartDate","{0:d}")%>

所以,当我们再需要用反射去获得对象的某个属性值的时候,不需要再如下写代码了:

public object GetPropertyValue(object obj,string propertyName)
{
    Type t 
= obj.GetType();
    PropertyInfo info 
= t.GetProperty(propertyName);
    
return info.GetValue(obj,null);
}

上面这样写虽然可以达到效果,但是麻烦,而且对于处理person.Contact.Address这种属性比较麻烦。可以用DataBinder.Eval如下写:

public object GetPropertyValue(object obj,string propertyName)
{
    
return DataBinder.Eval(obj,propertyName);
}

 

 

 <ItemTemplate>
              <table id="tblUserList" border="0" width="100%" cellpadding="0" cellspacing="1">
               <tr>
                <td valign="middle" width="150" bgcolor="#e5e7e8" align="right">
                 <table cellpadding="0" cellspacing="0" border="0">
                  <tr>
                   <td width="50" class='bodycopygreen' align="right" id='td_<%# DataBinder.Eval(Container.DataItem, "sno")%>'>&nbsp;&nbsp;&nbsp;<strong><%# DataBinder.Eval(Container.DataItem, "ChannelNumber")%></strong></td>
                   <td width="100" align="center"><a id='img<%# DataBinder.Eval(Container.DataItem, "sno")%>' onclick="alert('ddddd');" href='javascript:fnUserService(1,<%# DataBinder.Eval(Container.DataItem, "sno")%>,"<%# DataBinder.Eval(Container.DataItem,"ID")%>","<%# DataBinder.Eval(Container.DataItem,"Type")%>","<%# DataBinder.Eval(Container.DataItem,"PosterName")%>")'><img  style="cursor:pointer"  border=0 src='../<%# DataBinder.Eval(Container.DataItem,"IconName") %>' width="48" height="34" alt=""></a></td>
                  </tr>
                 </table>
                </td>
                <td valign="middle" width="600" bgcolor="#d9ecd9" id='tdUserList<%# DataBinder.Eval(Container.DataItem, "sno")%>'>&nbsp;&nbsp;&nbsp;&nbsp;<a  id='tda<%# DataBinder.Eval(Container.DataItem, "sno")%>' style="text-decoration:none" class='bodycopygreen' onclick="alert('ddddd');" href='javascript:fnUserService(2,<%# DataBinder.Eval(Container.DataItem, "sno")%>,"<%# DataBinder.Eval(Container.DataItem,"ID")%>","<%# DataBinder.Eval(Container.DataItem,"Type")%>","<%# DataBinder.Eval(Container.DataItem,"PosterName")%>")'><strong><%# DataBinder.Eval(Container.DataItem,"Name")%></strong>
                 </a>
                </td>
               </tr>
              </table>