Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting DateTime DataBinder Repeater problem

Posted on 2006-10-23
10
Medium Priority
?
667 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:mosArt
  • 5
  • 2
10 Comments
 
LVL 9

Expert Comment

by:william007
ID: 17791141
Try
public String FormatDateTime(DateTime date)
    {

        return date.ToShortDateString();
    }

You are getting error because return date.ToShortDateString(); is returing a string rather then datatime.
0
 
LVL 9

Expert Comment

by:william007
ID: 17791152
* returing a string rather then datatime.
returning a string rather then datetime value.
0
 

Author Comment

by:mosArt
ID: 17792148
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Expert Comment

by:william007
ID: 17793513
Hi, try to put a # there, see how it is going..
<%#FormatDateTime(DataBinder.Eval(Container.DataItem, "ItemID")); %>
0
 
LVL 9

Expert Comment

by:william007
ID: 17793561
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.
0
 

Author Comment

by:mosArt
ID: 17793988
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'.

0
 
LVL 9

Accepted Solution

by:
william007 earned 400 total points
ID: 17794034
Hmm...seems we are nearer now..try this
Option1:
<%#FormatDateTime(DateTime.Parse(DataBinder.Eval(Container.DataItem, "ItemID"))); %>

Option2:
Otherwise if you want to play safe,
public String FormatDateTime(String date)
{
       try{
         return DateTime.Parse(date).ToShortDateString();
       }
      catch(Exception e){
     //handle it
     }
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question