Jonas Stawski

Everything .NET and More

Container.DataItem. What exactly is this?

Recently I had to bind to an array of strings and I wasn't sure how to show the value of the current item. After a little bit of help from the DevTheo and a little bit of thinking I figured out the solution was very simple. All i had to do was bind it like this:

<%# Container.DataItem %>

So what is this expression exactly? The <%# %> means this is a DataBinding expression and Container.DataItem is an alias for the current item in the datasource. In other words, if you are binding to a collection of objects Container.DataItem is the current row of that collection.

If we use a DataView (default type when using SQLDatasource) to bind then Container.DataItem is a DataRowView. For a collection of objects of type Animals the type will be Animal and for an array of strings Container.DataItem will be a string.

Since .NET doesn't know to what type of collection object you will bind to DataItem will return an object. For C# you will have to cast your DataItem:

<%# ((DataRowView)Container.DataItem)["SomeProperty"] %>

There's another way of achieving the same result and that is using DataBinder.Eval.

<%# DataBinder.Eval(Container.DataItem, "SomeProperty")%>

DataBinder.Eval is a helper method that uses reflection to find the type of the DataItem in order to display the data correctly. The benefit of the Eval method is that if your type changes in the future you don't need to change the code, but in the other hand reflection causes performance to slow down.

Happy Programming!

Comments (28) -

Hey Now Jonas,
Interesting post, I'm glad I leaned something about Container.DataItem.
Thx 4 the info,
Catto

Reply

Hi Jonas,

I have an issue while am trying to get the value from datalist control.
I am using an datalist, in the item template i have a few labels and datalist which are binded with DataSource,
the child datalist should get a value from its patent datalist.

whether I can do it with container.dataitem????

any suggestions???

Reply

Manoj,
  Simply set the datasource of the inner datalist as datasource='<%# Container.DataItem("ColumnNameOrPropertyName") %>'

Reply

Hi Jonas,

I was wondering if you could help. I need to get the row id to pass via command argument. This is because my primary key consists of 7 columns, and I'd rather pass the row id.

Is it possible to get it from the Container.DateItem?
Thanks
Dan

Reply

Dan,
    Of course it is possible. You need to make sure the RowID is part of your datasource. Since you are talking about command argument, i take it you have a button, linkbutton (or something like that) within a repeater, gridview or datalist. Simply bind the rowid to the commandargument property of the control. Something like this:

<asp:button id="btn" runat="server" commandname="update" commandargument='<%# Container.DataItem("RowID") %>' />

Reply

Thank you so much! It's very very helpful! I was right stuck here.

Reply

hey thanks for the info.i was searching for the same.

Reply

Hi,
I have a problem and hope you guys will come out with a solution.

I have a group of controls(textboxes and labels). Now on click of a button I want the old group to be there and sam group to be shown just below the old one. With out a repeater the alignment wont be good.
Please suggest something and help me out.
Thanks

Reply

srikant,
    Try posting your question in the ASP.NET forums: http://forums.asp.net. You will have more visibility there as that is the right place to ask these king of questions.

Good Luck...

Reply

oh man, good

Reply

Hi,

What if I need to pass the entire data item (of an arbitrary type) to a child control?

Simply writing:-

<uc1:MyControl ... MyProperty="<% Container.DataItem %>" />

- doesn't work because ASP.NET turns the <% %> into a string, and my child class can't necessarily recreate the original object.

So far I've been doing it in the codebehind:-

Repeater1_ItemDataBound (...)
{
  (e.FindControl("MyControl") as MyControl).MyProperty = e.Item.DataItem as MyType;
}

- but it feels like there ought to be a better way. Any thoughts?

Reply

Jonas Stawski
Jonas Stawski

Iain,
      I do not think you can do this in the ASPX as ASP.NET converts the value from a string. The way you're doing it now in the code behind is the right way and there's nothing wrong with it.

Reply

Actually you can, that is with asp.net using vb
<%#DataBinder.Eval(Container.DataItem, "MyType")%>

Reply

Syed Mutahhar
Syed Mutahhar

nice .. i like it

Reply

hey,

Thanks 4d info.. This article is jus wat i was lookin for.. Me a fresher in coding n u have an excellent article..
Cheers to u Smile

Reply

junior4ever
junior4ever

Very nice

Reply

Nice post Jonas.

I was wondering how we can get a handle to a similar object (Container.DataItem) outside of <%# %> for example in <% %> with inline C#.

Reply

@Geeks
You will have to subscribe to the Row Data Bound event that the grid view fires after each row is bound. Read this article for more details: www.simple-talk.com/.../" title="www.simple-talk.com/.../">www.simple-talk.com/.../take-row-level-control-...

Reply

Hey…I have Go Daddy shared hosting. I was having problems so I called them and they are telling me that the System.Web.Routing name space is not available with a shared hosting plan and any dll that needs to be registered with Its is not allowed. What kind of hosting plan do you have with Go Daddy?

Reply

harshad bhola
harshad bhola

ok ok but what is diff between eval() and dataitem

Reply

@harshad bhola, the difference is explained in the blog post. "DataBinder.Eval is a helper method that uses reflection to find the type of the DataItem in order to display the data correctly," while Container.DataItem is the current object of the datasource.

Reply

Very good post. Thanks a lot. You explaining this things in vry entertaning way.

Reply

Thats really good Post. thank u so much

Reply

Ganesh Alias Premanath
Ganesh Alias Premanath

Superb article.Simple and Dynamic

Reply

Gaurang Naik
Gaurang Naik

Wonderful post. Thanks.

Reply

simple and understandable for beginners like me. Thanks.

Reply

Add comment

biuquote
Loading