Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Masking a bound datetime in FormView C#

Posted on 2011-02-17
11
Medium Priority
?
1,855 Views
Last Modified: 2013-12-17
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:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26828054.html
So is there another way of achieving a masked date & time for edit purposes?
0
Comment
Question by:forsters
[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
  • 6
  • 5
11 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 34922187
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
0
 

Author Comment

by:forsters
ID: 34924743
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.
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 34926205
Sorry, I tend to think in VB. On my mobile phone, so can't do as much but check out http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21275009.html and let me know if you need more info.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:forsters
ID: 34926461
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.

0
 
LVL 14

Expert Comment

by:quizwedge
ID: 34930393
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 http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_21672043.html For the hh:mm tt part, you'd use the formats found at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 34930403
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.
0
 

Author Comment

by:forsters
ID: 34932400
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.
0
 

Author Comment

by:forsters
ID: 34941744
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!
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 2000 total points
ID: 34945301
You're making me actually have to dig in and do some research and testing on this one instead of just going from memory. ;) Looks like it is simple, I just had the wrong syntax for this particular case. The code you're looking for is

<%# Bind("DateTime", "{0:dd/MM/yyyy hh:mm}") %>

Open in new window

0
 

Author Comment

by:forsters
ID: 34950428
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!
0
 

Author Closing Comment

by:forsters
ID: 34950431
spot on thank you!
0

Featured Post

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.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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