Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Masking a bound datetime in FormView C#

Is there a way of changing this:

<%#Bind("DateTime") %> so that what is displayed is either the date dd/mm/yyyy or time hh:mm (no seconds)

I have a JQuery function on other pages which is great but my edit page uses a FormView which blocks the JQuery from seeing the controls inside...see my other question on this:
https://www.experts-exchange.com/questions/26828054/using-JQuery-mask-for-items-inside-FormView.html
So is there another way of achieving a masked date & time for edit purposes?
Avatar of quizwedge
quizwedge
Flag of United States of America image

I haven't done anything with JQuery, but the following should work

<%# FormatDateTime(Bind("DateTime"), [format]) %>

Open in new window


You can see different formats at http://www.dotnetspider.com/resources/21593-FormatDateTime-function-VB-NET.aspx

You could also use

<%# CType(Bind("DateTime"), DateTime).ToString([format]) %>

See all or at least a lot of the formats at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Avatar of forsters

ASKER

Hi thanks for the reply,

That to me looks like VB code, it's exactly what I want but I need it in C# equivalent.
Sorry, I tend to think in VB. On my mobile phone, so can't do as much but check out https://www.experts-exchange.com/questions/21275009/FormatDateTime-in-C.html and let me know if you need more info.
No worries, just grateful for the help, frustrating how such tiny details can have you stumped for days.

thanks for the link, I found that in google too and thought I was onto something but because i'm trying to format a bound textbox it errored on the Bind.

This is what I tried - I may have just got the syntax a bit messed up?

 <asp:TextBox ID="TxtDeadlineTime" Text='<%#DateTime.Parse(Bind("DeadlineTime").ToString("T")) %>' runat="server"/>

if you can help anymore i'd really appreciate it when you get a chance.

I believe the syntax should be

<asp:TextBox ID="TxtDeadlineTime" Text='<%#DateTime.Parse(Bind("DeadlineTime")).ToString("T") %>' runat="server"/>

Open in new window


Looks like a misplaced closing parenthesis. I also found the following format that could help

<asp:TextBox ID="TxtDeadlineTime" Text='<%#Bind("WOTime").ToString("hh:mm tt")%>' runat="server"></asp:TextBox>

Open in new window


Found that at https://www.experts-exchange.com/questions/21672043/Formatting-Datetime-in-formview.html For the hh:mm tt part, you'd use the formats found at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Realized I made a mistake in my second code sample. It should say Bind("DeadlineTime") not Bind("WOTime"). Just the field name and figured you'd probably catch it, but I tried to make it as copy and paste friendly as possible.
Hi thank you I will try both - think I may have tried shifting the parenthesis but I will try again just in case I did something silly (high possibility).
Thanks for this will report back.
D'oh neither work, on the first one I get:

"The name 'Bind' does not exist in the current context"

the second one I get an error:

" A call to Bind was not well formatted.  Please refer to documentation for the correct parameters to Bind"

Any more ideas, it seems such a basic requirement I can't believe there isn't a way of doing it in C#, someone has suggested a way of getting the JQuery code to work so i'll see if that delivers any joy but it would be nice to get both methods working...don't like to let the machines win!
ASKER CERTIFIED SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America image

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
Haha always good to know us newbs are making the pros work a little.

Well you'll be chuffed to know that's nailed it thank you very much and for anyone else out there this is what I now have:
<asp:TextBox ID="TxtDeadlineDate" Text='<%# Bind("DeadlineDate", "{0:dd/MM/yyyy}") %>' runat="server"/>
which displays in my textbox as: 12/04/2011
AND
<asp:TextBox ID="TxtDeadlineTime" Text='<%# Bind("DeadlineTime", "{0:hh:mm}") %>' runat="server"/>
which displays in mytextbox as: 12:00

Perfect thank you so much you've been a complete star!
spot on thank you!