When to use

T

tshad

I am confused as to when to use "Container.DataItem" and when to use
DataBinder.Eval(Container.DataItem, xxx)"?

In my VB.Net pages I use Container.DataItem.

In one of my pages I have the following (where RunProcedure just sends back
a DataReader) where I use DW:
***************************************
dbReader = myDbObject.RunProcedure("GetJobPostingsByJobTitle", parameters)
InProgressGrid.DataSource = dbReader
InProgressGrid.DataBind()
....
<asp:DataGrid
AutoGenerateColumns="false"
ID="InProgressGrid"
runat="server" >
<columns>
<asp:TemplateColumn Visible="false">
<itemtemplate>
<asp:Label ID="PositionID" Text='<%#
Container.DataItem("PositionID")%>' runat="server"/>
</itemtemplate>
</asp:TemplateColumn>
</columns
</asp:DataGrid>
************************************************

In this one I use "Container.DataItem("PositionID")" to get the data during
the DataBind.

But when I was creating this in VS 2003 C#:

*************************************************************
DataGrid1.DataSource = cmd.ExecuteReader();
DataGrid1.DataBind();
....

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 88px; POSITION:
absolute; TOP: 48px" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="The Title">
<ItemTemplate>
<asp:Label ID="title" Text='<%# DataBinder.Eval(Container.DataItem,
"title") %>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
***************************************************************

I had to do "DataBinder.Eval(Container.DataItem, "title")" instead of
"Container.DataItem["title"]" because the Container.DataItem["Title"] gave
me an error:

CS0021: Cannot apply indexing with [] to an expression of type 'object'

What is the problem here? Is it because of C# instead of VB.Net?

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top