Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataBinder.Eval and Container.DataItem descriptions

Posted on 2004-09-19
6
Medium Priority
?
6,982 Views
Last Modified: 2010-05-18
I need description of ASP QuickStart tutorial fragment:

http://samples.gotdotnet.com/quickstart/aspplus/doc/webpagelets.aspx
Encapsulating Events in a User Control
Click View Source, CS\Pagelet6.ascx.

This file contains the following code:

<b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>

Please explain how does it works. Thanks.

0
Comment
Question by:AlexFM
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12096293
Hi AlexFM,

when DataBind() method is called the datalist control has been binded
the DataBinder.Eval method gets the Container.DataItem object
the "title" value of the second property of DataBinder.Eval method is the navigation path, so the Eval method to know what is searching for

when you use the Databinder.eval method for datalist, repeater or datagrid control the container.Dataitem is a row from the datasource, and second parameter is the name of the column, so for the current sequence asp.net to know which value has to show


B..M
0
 
LVL 48

Author Comment

by:AlexFM
ID: 12099677
DataBinder.Eval is static mathod of the DataBinder class. What is Container? Property of some class?
0
 
LVL 7

Expert Comment

by:gsiric
ID: 12099970
Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list.

For DataTable or DataView as the DataSource, Container.DataItem is  DataRowView.
For DataReader as the DataSource it is DbDataRecord.

DataBinder.Eval is helper method that use reflection (late binding) to get you right data.
Instead of using DataBinder.Eval you can directly cast Container.DataItem to right type and get data.

in your example you can replace

   <b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>

wiht:

  <b>Title: </b><%# ((System.Data.DataRowView)Container.DataItem)["title"] %><br>


0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 48

Author Comment

by:AlexFM
ID: 12100081
This line belongs to the DataList control:

<ASP:DataList id="MyDataList" BorderWidth="0" RepeatColumns="2" runat="server">
    <ItemTemplate>
        <table cellpadding="10" style="font: 10pt verdana">

         ...
                    <%# DataBinder.Eval(Container.DataItem, "title") %>
        ...

        </table>
    </ItemTemplate>
</ASP:DataList>

It's DataSource is set by the this code:

DataSet ds = new DataSet();
...
MyDataList.DataSource = ds.Tables["Titles"].DefaultView;

What is runtime alias? I try to find Container property of some WebForms class, without success. Your description helps, but I think I still missing some details.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12100117
when you bind like this
at runtime Container.DataItem is DataRowView
why do you need the runtime alias ?

B..M
0
 
LVL 7

Accepted Solution

by:
gsiric earned 2000 total points
ID: 12100381
DataGrid and Repeater have DataSource property.

DataSource can be DataTable, DataView, etc.. , and any component that implement IList or IListSource interface.
This means dat items which will be displayed in DataGrid or repeater can be of any type.

For example you can declare your own class (for example Person), and have array of objects of this class as a data source.

Person[] arrPersons = ....
DataGrid.DataSource = arrPersons;

How data is displayed in the grid ?
When DataGrid is rendered, it takes items one by one and using your code in ItemTemplate or AlternatingItemTemplate
display data.
Because you are using templates, there must be name to represent current item. (Person, DataRowView, ... )
Container.DataItem representst this current item, and it is of Object type.
So if your data source is array of Persons, Container.DataItem is current Person.
In case when the data source is DataTable, current item is DatRowView.
DataBinder.Eval is just helper function to evaluate data, and it use reflection to find right property in your item.
You can display data without using DataBinder.Eval, but you must cast Container.DataItem to right type, and then
access propeties of object.

Your code for data binding must finish in real code, so:

<%# DataBinder.Eval(Container.DataItem, "title") %>

can be translated at runtime in something like this:

public string SomeMethodFromDataBinding()
{
     // obtain container and current item
      object Container = GetContainer();
      object  Container.DataItem = GetCurrentItem();
   
      // here your code from template finish
     // Container and DataItem are defined before inserting your code here

      object  result =  DataBinder.Eval(Container.DataItem,"title");
     
     return object.ToString();
}










0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question