Solved

DataBinder.Eval and Container.DataItem descriptions

Posted on 2004-09-19
6
6,975 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 57
VB.Net creating Contact in Outlook 1 96
Nested forach loop to linq 3 42
Manage big list of parameter list 8 21
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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