Solved

DataBinder.Eval and Container.DataItem descriptions

Posted on 2004-09-19
6
6,955 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now