Link to home
Start Free TrialLog in
Avatar of mosArt
mosArt

asked on

Formatting DateTime DataBinder Repeater problem

Got this in a repeater: <%# DataBinder.Eval(Container.DataItem, "ItemStartDate")%>

Outputs: 16.10.2006 00:00:00

Wants: 16.10.2006

Trying:
<% FormatDateTime(DateTime DataBinder.Eval(Container.DataItem, "ItemStartDate")) %>

//code below is in .cs file:
public DateTime FormatDateTime(DateTime date)
    {

        return date.ToShortDateString();
    }

But I´m only getting:  CS0029: Cannot implicitly convert type 'string' to 'System.DateTime'


...
Line 145:        return date.ToShortDateString();
....

Can Anyone please tell me why?
Avatar of william007
william007

Try
public String FormatDateTime(DateTime date)
    {

        return date.ToShortDateString();
    }

You are getting error because return date.ToShortDateString(); is returing a string rather then datatime.
* returing a string rather then datatime.
returning a string rather then datetime value.
Avatar of mosArt

ASKER

It kinda worked, but it only got me half way there...

Can I send in the databinder to the method?

Trying:
<%FormatDateTime(DataBinder.Eval(Container.DataItem, "ItemID")); %>

Error:
CS0103: The name 'Container' does not exist in the current context

Is there another way?
Hi, try to put a # there, see how it is going..
<%#FormatDateTime(DataBinder.Eval(Container.DataItem, "ItemID")); %>
See here http://samples.gotdotnet.com/quickstart/aspplus/doc/webformssyntaxref.aspx#syntax

Rendering Code Syntax: <% %> and <%= %>
Code rendering blocks are denoted with <% ... %> elements, allow you to custom-control content emission, and execute during the render phase of Web Forms page execution.

Data Binding Syntax: <%# %>
Code located within a <%# %> code block is only executed when the DataBind method of its parent control container is invoked.

So when the rendering code syntax is running, the container is not there yet..so try to use the databinding syntax.
Avatar of mosArt

ASKER

Also good advice, but no sigar...

CS1502: The best overloaded method match for 'Default_aspx.FormatDateTime(System.DateTime)' has some invalid arguments
...
Line 164:   <%# FormatDateTime(DataBinder.Eval(Container.DataItem, "ItemStartDate"))%>
...

I cannot move the # infront of DataBinder.Eval, 'couse it have to be the first 'letter'.

ASKER CERTIFIED SOLUTION
Avatar of william007
william007

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial