Solved

Formatting DateTime DataBinder Repeater problem

Posted on 2006-10-23
10
603 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
Comment Utility
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
Comment Utility
* returing a string rather then datatime.
returning a string rather then datetime value.
0
 

Author Comment

by:mosArt
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

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

Expert Comment

by:william007
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now