Solved

DataBinder.Eval and Container.DataItem descriptions

Posted on 2004-09-19
6
6,978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

636 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