Data Binding Expression Syntax

  • Thread starter Thread starter JV
  • Start date Start date
J

JV

I just converted a big web project from VB.NET to C# and I got some very
strange runtime errors. Apparently, throughout the ASPX pages, the VB
version of this project used the expression <%#
Container.DataItem("columnName") %> inside an <ItemTemplate> tag for
DataGrid. Apparently though this works fine in VB.NET, for some reason C#
chokes on it. I have found that replacing those expressions with <%#
DataBinder.Eval(Container.DataItem, "columnName")%> makes the errors go
away, but I can't figure out why. It seems to me that the evaluation of
data binding expressions embedded in the ASPX page should work identically
in both languages?

Can anyone explain the arcane rules of this syntax?
 
You should typecast Container. In the case of datagrid it will look like

<%# (DataRowView)Container.DataItem("columnName") %>

DataBinder.Eval uses reflection at runtime and, therefore, is much slower.

Eliyahu
 
Tried that first, got another error.

Sharon said:
Maybe try:
<%# Container.DataItem["columnName"] %>

JV said:
I just converted a big web project from VB.NET to C# and I got some very
strange runtime errors. Apparently, throughout the ASPX pages, the VB
version of this project used the expression <%#
Container.DataItem("columnName") %> inside an <ItemTemplate> tag for
DataGrid. Apparently though this works fine in VB.NET, for some reason C#
chokes on it. I have found that replacing those expressions with <%#
DataBinder.Eval(Container.DataItem, "columnName")%> makes the errors go
away, but I can't figure out why. It seems to me that the evaluation of
data binding expressions embedded in the ASPX page should work
identically
in both languages?

Can anyone explain the arcane rules of this syntax?
 
C# is morte typesafe than VB as does not support late binding (except thru
the .net reflection api). the expression inside <%# %> must be a valid C$
expression amd return a string. for most binding objects, an object is
returned. aslo if you use an indexer, you must switch from () to []. the
DataBinder.Eval was written for C# to help with handling this and some late
binding issues.

-- bruce (sqlwork.com)
 
Bruce,

In all Net languages is a procedure stated with (). In VBNet is that as well
for a index from a collection or an array where that is in the C languages
[].

Most VBNet programmers have Option Strict On in there IDE by what is dynamic
binding of an object prevented in the code. (Late binding by reflection is
in all languages allowed).

For all the dotNet languages is the code for the Databinder Eval exact equal
when it is done in the ASPX part.

http://msdn.microsoft.com/library/d...frlrfsystemwebuidatabinderclassevaltopic1.asp

Cor
 
And actually that generates an error as well:

Compiler Error Message: CS0246: The type or namespace name 'DataRowView'
could not be found (are you missing a using directive or an assembly
reference?)
 
Well, I think it's apparent that there is still a lot of confusion about
this, so it is not clearly enough documented.

What doesn't make sense to me is what the VB application works fine with
just <%# Container.DataItem("someName") %> when the C# one does not. If
that is meant to be array syntax, then one would think that <%#
Container.DataItem["someName"] %> would work, but it does not.
 
That's a great article. I'm still puzzled why I had trouble with the
following syntax even though I've seen it recommended:

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

This generated an error for me.
 
JV,
<%# ((DataRowView)Container.DataItem)["EmployeeName"] %>
This means that you use as datasource from by instance a DataList a
DataRowView, which is a row of a Dataview or with another name
datatable.defaultview. I don't think that you are doing that.

Cor
 

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

Back
Top