Solved

Formatting DateTime DataBinder Repeater problem

Posted on 2006-10-23
10
650 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
using CK editor in iframes 5 35
Asp.Net Session Question 2 49
asp.net, radiobuttonlist, c# 3 69
Can Selenium do Load Testing? 2 61
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

751 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