Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please explain the parts of a DataBinder.Eval() for me

Status
Not open for further replies.

InsideEdge

Instructor
Apr 5, 2005
35
US
Hi, I’m teaching myself Visual Studio.NET and I came across an example in my book that looks like this:

<SelectedItemTemplate>
&nbsp;
<asp:HyperLink id=HyperLink2 runat="server" CssClass="DepartmentSelected" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'
NavigateUrl='<%# "../default.aspx?DepartmentID=" &amp; DataBinder.Eval(Container.DataItem, "departmentID") &amp; "&amp;DepartmentIndex=" &amp; Container.ItemIndex %>'>
</asp:HyperLink>
</SelectedItemTemplate>

This is a hyperlink that is in a dataList control. There is an underlying database that the control reads from. The part that I’m confused about is the elements “Name” and “departmentID” of DataBinder.Eval(). Are those elements that should match strings from my database? Or can I just use any string here?

Thanks for your help with this one,

AlexanerBlade
 
>>Are those elements that should match strings from my database?

those are the column names whose info must be displayed from the DataSource...

Known is handfull, Unknown is worldfull
 
When the you bind data, there's an item pertaining to the given row/item. For example, if you're binding to a DataTable/DataSet the item is a DataRowView. If you're binding to a SqlDataReader the item is an IDataRecord (I think) while if you're binding to an array of your own Product objects, the item would be a Product.

The "item of data" for the given grid or list item is the Container.DataItem part of the equation (indirectly).

What DataBinder.Eval does is treat that item of data as a typeless object and it looks for a specific bit of data within the item of a certain name.

For example, if you're binding to a SqlDataReader and the resultset contains the database column "ItemName". Then DataBinder.Eval( Container.DataItem, "ItemName" ) would examine the IDataRecord looking for a field named "ItemName".

Likewise if you had your own Product object with a Price property and it was the DataItem, then DataBinder.Eval would reflect into the object's properties looking for one named "Price".

You can learn more about it here:
...and learn about a 2.0 enhancement to the system here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top