Setting DataFormatString on a textbox

Discussion in 'ASP .Net Building Controls' started by TS, Feb 5, 2009.

  1. TS

    TS Guest

    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
     
    TS, Feb 5, 2009
    #1
    1. Advertising

  2. 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:

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

    Aspx.cs:
    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.

    Regards,
    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:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    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
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Feb 6, 2009
    #2
    1. Advertising

  3. Hi TS,

    Have you tested my code? Can it work?

    Regards,
    Allen Chen
    Microsoft Online Community Support
     
    Allen Chen [MSFT], Feb 10, 2009
    #3
  4. TS

    TS Guest

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

    thanks!

    "Allen Chen [MSFT]" <> wrote in message
    news:p...
    > Hi TS,
    >
    > Have you tested my code? Can it work?
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Community Support
    >
     
    TS, Feb 13, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frank

    DataFormatString Doesn't Work

    frank, Sep 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,150
    Luc Kumps
    Sep 5, 2003
  2. victor nguyen
    Replies:
    1
    Views:
    4,162
    Rutger Smit
    Sep 11, 2004
  3. Kenny

    How to use DataFormatString?

    Kenny, Dec 30, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    970
    John Saunders
    Dec 31, 2004
  4. Kenny

    DataFormatString

    Kenny, Dec 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,693
    Karl Seguin
    Dec 31, 2004
  5. Tarren
    Replies:
    7
    Views:
    5,046
    Tarren
    Jan 20, 2005
Loading...

Share This Page