Buscar

Últimas inserções

Gerando planilhas XLS para Excel com formatação de colunas
MD5 direto no Sql Server
Aplicação demora muito para inicializar quando VisualStudio está "Loading Symbols"
Fazendo Painel de Leds em menos usando System.Drawing(parte 3)
Fazendo Painel de Leds em menos usando System.Drawing(parte 2)
- TODAS AS DICAS

Top Poggers (sempre)

Jar Jar Binks (136)
Cuban Pete (127)
Tufo (28)

Top Poggers (30 dias)


Área restrita

Logon

pogMasters.NET

Início
Busca Avançada
Contato

RSS
 

.NET - Como usar DataBinder.Eval e Container.DataItem

Outros

24/04/2008 por Jar Jar Binks


Reposting here for the benefit of asp.net developers and Google

The databinding expression <%# some expression %> is evaluated in the language of the page (VB, C#, etc.) This can have a big impact on the current syntax, so be very careful when you are looking at docs for the language you are using.



Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list. For a grid which displays 10 rows of data, this is one row from the datasource. The actual type of DataItem is determined by the type of the datasource. For example, if the datasource is a Dataview, the type of DataItem is DataRowView. If the type of the datasource is an array of strings, the type of DataItem is String. If the datasource is a collection of strongly-typed objects (for example "Employees" objects), the type of DataItem is Employees.



Each of these cases requires a slightly different databinding expression, with further differences between VB and C#. In every case, you want the databinding expression to produce a string that can be displayed in the page.



Here are some examples:



Array of Strings:

VB/C# <%# Container.DataItem %>



Field from DataView:

VB <%# Container.DataItem("EmployeeName") %>

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



Property from a collection of objects:

VB <%# Container.DataItem.CustomerName %>

C# <%# ((Customer)Container.DataItem).CustomerName %>



Non-String property from a collection of objects:

VB <%# CStr(Container.DataItem.CustomerID) %>

C# <%# ((Customer)Container.DataItem).CustomerID.ToString() %>





As you can see the syntax is tricky, especially for C#, which requires explicit casting. So we've provided a DataBinder.Eval() helper method that figures out the syntax for you and formats the result as a string. It's really convenient, with a couple of caveats: it's late bound (uses reflection at runtime to figure out the data types), and it only supports basic data types in the fields: string, int, datetime, etc.

You can use Eval instead of DataBinder.Eval in ASP.net 2.0



DataBinder.Eval takes 2 or 3 arguments. The first arg is the data object to bind to. In the case of DataGrid, DataList and Repeater, Container.DataItem is the single row. The second arg the string name of the field from the data object you which to display. DataBinder.Eval uses these two pieces of information to work out the rest of the expression syntax.



An optional third argument is a .NET formatting string. DataBinder.Eval will handle a single replacement for {0} only. So the example below:



<a href='<%# "default.aspx?CategoryId=" + Cstr(Databinder.Eval(Container.DataItem, "ID"))%>'>


could be simplified to:


<a href='<%# Databinder.Eval(Container.DataItem,"ID","default.aspx?CategoryId={0}" ) %>'>



Wrapping DataBinder.Eval in CStr() is unnecessary as the compiler will wrap the statement with a Convert.ToString like this:

control1.SetDataBoundString(0, Convert.ToString(DataBinder.Eval(item1.DataItem, "ID")));



Best of all, the Databinder.Eval syntax is the same for VB and C#.


De weblogs.asp.net

*** Dica Postada por POG MASTERS DESKTOP - v1.0
 
   


Comentários sobre o artigo

25/04/2008 por zé

> num tendi nada!

22/07/2008 por Jão

> Quer que traduza?

29/01/2009 por David

> How do you translate [Container.DataItem.Row.GetChildRows("MsgComments")]

to c#?

12/03/2009 por Clayton

> It Depences on the type of your DataItem Object

12/03/2009 por Clayton

> Como será que eu faço pra, no design, dar bind numa propriedade tipo objeto , via markup?

13/03/2009 por Clayton -> To David

> Hello David,
As you asked in , about how to translate the code ''Container.DataItem.Row.GetChildRows("MsgComments")]' into C#, I imagine you have to specify the type of your DataItem element, such as:
'(myObjectType) Container.DataItem'
doing so , you made a cast of the DataItem into the object you want to get the "Rows" from.

((myObjectType)Container.DataItem).Row.GetChildRows("MsgComments")

I hope this message was useful,
Regards,

Clayton (Jar Jar Binks)

03/12/2009 por Lewi

> Thanks a lot of this article!!

16/07/2010 por Jar jar Binks

> POstando a referencia do artigo:
http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx

23/08/2011 por Desconhecido

> Copiar e colar tenso!

Poste um comentário >>
Nome  
Email
Comentário    
Postar Comentário