Solved

DataBinder.Eval and Container.DataItem descriptions

Posted on 2004-09-19
6
6,965 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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