473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Databinder.eval( ) mystification

a general question...
I'm a bit mystified by Databinder.eval(object, Colname, [format])

if controls can be bound to their individual data sources within the load
event of the page...
why would we ever use this quite inelegant OO code wrapped in #<% %> inside
the markup?

secondly:
sometimes you see simliar code inside the markup without calls to
Databinder.eval that everyone says avoid as it uses reflection...what is that
second form?

finally:
is it a question of elegance/inelegance, or are there things that
Databinder.eval() can do that can't be done any other way in code behind.

Thanks in advance and regards,
CharlesA
Jan 19 '06 #1
4 7327
Controls can't be bound to their values in Page_Load during a databound
operation - specifically what Databinder.Eval is meant to do. They can be
set in the ItemDataBound event however.

The 2nd form is to cast the Container.DataItem to the type of the underlying
datasource. So if you are binding to a DataSet, DataTable or DataView, the
underlying row type is DataRowView, which you can use to do:

c#
((System.Data.DataRowView)Container.DataItem)["FirstName"]
vb.net
ctype(Container.DataItem, System.Data.DataRowView)("FirstName")

if you were binding to a collection of "User", you would cast it to "User"

DataBinder.Eval DOES use reflection. It DOES cause a performance hit. BUT,
it's implementation agnostic of how your business layer is implemented.
DataBinder.Eval doesn't care if you are using a rich domain model or table
modules (ie, datasets). It let's you change some of your business layer/data
access layer without needing to also change your presentation layer. In
that, it's absolutely wonderful.

I would recommend that you always use DataBinder.Eval, unless you've done
some specific profiling and found it to be a performance hog in your
scenario. Otherwise it's a micro-optimization which sacrifices some nice
low-coupling.

Karl

--
http://www.openmymind.net/

"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
a general question...
I'm a bit mystified by Databinder.eval(object, Colname, [format])

if controls can be bound to their individual data sources within the load
event of the page...
why would we ever use this quite inelegant OO code wrapped in #<% %>
inside
the markup?

secondly:
sometimes you see simliar code inside the markup without calls to
Databinder.eval that everyone says avoid as it uses reflection...what is
that
second form?

finally:
is it a question of elegance/inelegance, or are there things that
Databinder.eval() can do that can't be done any other way in code behind.

Thanks in advance and regards,
CharlesA

Jan 19 '06 #2
Thanks Karl!
I got very nearly all of that (thanks especially for reminding me that the
second form just didn't have the databinder.eval and only the
container.dataitem) and I particularly got the casting bit if you use your
own custom objects...

however you wrote (and I didn't really get this):
Controls can't be bound to their values in Page_Load during a databound
operation - specifically what Databinder.Eval is meant to do. They can be
set in the ItemDataBound event however.
and I'm not sure what that paragraph means, could you elaborate a bit more
please?

I know that the itemdatabound event is called after the ItemCreated event
for the DataGridItem, but I thought all controls could have a data source if
they had runat="server"

Thanks again
Regards
CharlesA
Jan 19 '06 #3
well, if you have:

<asp:literal id="FirstName" runat="server" />
sure you can go in Page_Load and do:

FirstName.Text = Whatever;

but if that's in a bound control, ala:
<ItemTemplate>
<asp:literal id="FirstName" runat="server" />
</ItemTemplate>
you can't do the same thing in Page_Load

FirstName doesn't exist in the context of the page, only as a child control
of your repeater/datagrid/datalist.

Your only 2 choices is to do:
<asp:literal id="FirstName" runat="server"><%#
DataBinder.Eval(Container...)%></asp:literal>

or hook into the ItemDataBound and do (pseudo code)

if e.Item.itemType = Item orelse e.Item.ITemType = Alternatingitem then
dim firstName as Literal = e.Item.FindControls("FirstName")
firstName.Text = ctype(e.Item.DataItem, DataRowVIew)("FirstName") 'or
you could use DataBinder.Eval in here as well)
end if

between the two solutions, the %# is way simpler, and I'd only go into
ItemDataBound if there was some more complex logic to do.

The point is, you can't do this during Page_Load, because the datasource
isn't applied to the parent control (repeater/datalist/datagrid) during
page_load but rather during databinding..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
Thanks Karl!
I got very nearly all of that (thanks especially for reminding me that the
second form just didn't have the databinder.eval and only the
container.dataitem) and I particularly got the casting bit if you use your
own custom objects...

however you wrote (and I didn't really get this):
Controls can't be bound to their values in Page_Load during a databound
operation - specifically what Databinder.Eval is meant to do. They can be
set in the ItemDataBound event however.
and I'm not sure what that paragraph means, could you elaborate a bit more
please?

I know that the itemdatabound event is called after the ItemCreated event
for the DataGridItem, but I thought all controls could have a data source
if
they had runat="server"

Thanks again
Regards
CharlesA

Jan 19 '06 #4
Karl
you're a top man!
Thanks for that thorough and great explanation, it's cleared a big piece of
confusion for me!

thanks again and warm regards
CharlesA
Jan 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3803
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
2
2029
by: dm_dal | last post by:
I have a control on my webform that I am binding to a dataset. The issue is, the field value in the dataset is encrypted and I am trying to decrypt it during the binding process: Example: ...
5
724
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash...
4
2478
by: Søren Lund | last post by:
Hello, I am trying to bind a DataGrid with some data from a DataSet which contains fields in the form "group.fieldname". I am certain that my data source contains the field but I cannot get my...
1
2224
by: Mike Lerch | last post by:
Pretty much, TSIA, but I'll expand a bit: I'm still trying to get my head around the n-tier approach to web design. It seems to me that when you use DataBinder.Eval in the ASPX that your tiers...
2
20636
by: jack | last post by:
Hello, I have a date in a SQL Database and want to use the dateformat tools to format the output. I am now using the Month, Day, and Year to do it now. I want to change: Released Date:...
3
2251
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
1
1992
by: Nathan Sokalski | last post by:
I want to make an email link using databinding. I am able to get and display the email address from the database it is stored in using databinding as follows: <asp:HyperLink id="lnkEmail1"...
1
2149
by: RobASPNewGuy | last post by:
Hi, I'm new to this forum and hope I can get some help. I would like to make life easier by combining a appSetting (global variable set in the web.config file) and DataBinder.Eval to create a...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.