DataBind

G

Guest

I'm trying to figure out what is wrong with this statement...

Text='<%# DataBinder.Eval(Container.DataItem, "EventStartDate", "{0:d}")%>'

This is in a textbox. I'm using the exact format of this statement as a
stand along value (not embedded in an ASP control), but when I place it into
the Textbox control, I get an error as follows: 'System.Web.UI.Control' does
not contain a definition for 'DataItem'.

The format within my control looks OK to me, here is the code for the entire
control

<asp:TextBox ID="txtEditEventStartDate" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "EventStartDate",
"{0:d}")%>'></asp:TextBox><br>

But There must be something wrong that I do not see.
 
K

Karl

Jim,
You can only use databinding syntax within a databound control (repeater,
datalist, datagrid, dropdownlist, ...). From the error, it seems that you
have a free flowing texbox with no parent control.

For example, instead of having:

<asp:repeater id="repeater" runat="Server">
<itemtemplate>
<asp:textbox id="date" runat="Server" Text='<%#
DataBinder.Eval(Container.DataItem, "EventStartDate", "{0:d}")%>' />
</itemtemplate>
</asp:repeater>

and binding the repeater to a datasource, you simply have:
<asp:textbox id="date" runat="Server" Text='<%#
DataBinder.Eval(Container.DataItem, "EventStartDate", "{0:d}")%>' />

Understanding what's going on is important, so let's look at this line:
DataBinder.Eval(Container.DataItem, "EventStartDate", "{0:d}")

DataBinder.Eval -->
This is simply a helper object which let's you do late-binding...basically
it figures out what Container.DataItem is and how to get "EventStartDate"
out of it and into your specified format

Container.DataItem -->
This is saying get the Container (which in the first example is the repeater
row)'s dataitem. This is where you are going wrong.

I'd like to see more code and html to get a context for what you are trying
to do.

Karl
 
G

Guest

Karl, thanks for taking the time to explain this to me. You are exactly
correct, I am attempting to bind to a textbox which is free-standing. It
would have never occured to me that I could not bind to a column in a
database, but your explantion sure clarifies why I can not do this and it
makes perfect sense.
 

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

Top