locked
[DisplayFormat(DataFormatString = "{0:D}")] as C# Behind Code? RRS feed

  • Question

  • User-326746839 posted

    Hi,

    Is there a way of writing this sentence

    [DisplayFormat(DataFormatString = "{0:D}")]
    public oObject OrderDate;

    which goes written as a tag in an ASP.Net MetaData web form buddy file

    but writing it instead inside the "behind code" as a C# sentence instead?

    Carlos N. Porras
    (El Slavador) 

    Wednesday, April 17, 2013 12:59 PM

Answers

  • User281315223 posted

    You can use the String.Format() method to format the text of any type of object (assuming that it can be accessed as a string) that you pass into it as seen below : 

    //Example object (This is your object)
    YourObject yourObject = new YourObject();
    
    //Format your object as a String
    string formattedString = String.Format("{0:D}",yourObject)

    If you are using a Date object

    If you want to use that particular formatting string to format a Date in the code behind, you could use the DateTime.ToString() method :

    //Example Date
    DateTime orderDate = DateTime.Now;
    //Formatting
    string formattedDate = orderDate.ToString("D");

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2013 1:06 PM

All replies

  • User281315223 posted

    You can use the String.Format() method to format the text of any type of object (assuming that it can be accessed as a string) that you pass into it as seen below : 

    //Example object (This is your object)
    YourObject yourObject = new YourObject();
    
    //Format your object as a String
    string formattedString = String.Format("{0:D}",yourObject)

    If you are using a Date object

    If you want to use that particular formatting string to format a Date in the code behind, you could use the DateTime.ToString() method :

    //Example Date
    DateTime orderDate = DateTime.Now;
    //Formatting
    string formattedDate = orderDate.ToString("D");

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2013 1:06 PM
  • User-326746839 posted

    Thank you .... you are the best !

    Carlos N. Porras
    (El Salvador) 

    Wednesday, April 17, 2013 1:49 PM