Solved

Formatting DateTime DataBinder Repeater problem

Posted on 2006-10-23
10
654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 100 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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