Setting DataFormatString on a textbox



Hello, my project has a custom GridView control (inherited from GridView)
that uses TemplateFields to dynamically add controls based on meta data.
Since they aren't BoundFields I am now having to set DataFormatString on
each individual control but some controls dont have this option. For
DropDownList (and other ListControls) I have DataTextFormatString, but for
textbox and Label, etc i dont know how to set the format string.

for example, i have a textbox that when shown in read mode should show only
the date component of a datetime, so i want to do something like this
{0:MM/dd/yyyy} but dont know how

Allen Chen [MSFT]

Hi TS,

From your description you have a TextBox in ItemTemplate of the GridView
that shows a date field and want to format the text, right? If so I think
you can try this:

<asp:TextBox ID="TextBox1" runat="server"
Text='<%#DateFormat(Eval("theDate")) %>'></asp:TextBox>

protected string DateFormat(object input)
DateTime dt = DateTime.Parse(input.ToString());
string output = dt.ToString("MM/dd/yyyy");
return output;

Please have a try and let me know if it works.

Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

Get notification to my posts through email? Please refer to

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.

Allen Chen [MSFT]

Hi TS,

Have you tested my code? Can it work?

Allen Chen
Microsoft Online Community Support


thanks Allen, I found a different way to handle that in the binding method I
override in gridViewTemplate.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads