Need help with translating c# to javascript for client-side data binding

4 posts, 0 answers
  1. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 24 Mar 2009 Link to this post

    I'm moving the data binding for a RadGrid from the server side to the client side using a Web service. One of my columns has a value that is formatted by another value in the same row.  I was doing this on the server side and passing in the value of the column and the format column:

            protected static string FormatMetric(object oValue, object oFormat) { 
                string f = oFormat.ToString(); 
                try { 
                    double v = Convert.ToDouble(oValue); 
                    return v.ToString(f); 
                catch { 
                    return ""

    The format was a string like "$#,### mil".  How can I translate that to javascript?  I can't seem to figure out how to format a number using a custom format.
  2. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 26 Mar 2009 Link to this post

    Here's part of your solution (I think):

    With ASP.NET AJAX client-side libraries, you can actually use String.Format() to format values. For instance, you could write JavaScript code like this:

    var result = String.format("{0:d}",123);

    For more details on the JavaScript String.format(), check out this URL:

    Hope that helps some. Let me know if you need more pointers.

  3. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 27 Mar 2009 Link to this post

    Yeah, you'd think something like that would work, but that only works for the standard formats.  It doesn't work for custom format strings on numeric values for some reason.  Oh well.  I figured out a work-around.  I just added a formatted version of the field to the dataset as another field.  So the formatting is done in the WebMethod after retrieving the values from the database but before returning the dataset to the page.
  4. Nikolay Rusev
    Nikolay Rusev avatar
    2287 posts

    Posted 30 Mar 2009 Link to this post

    Hello sypher,

    Unfortunately not all data format strings are supported by ASP.NET AJAX client API.
    You can find list of supported formats below:
    Supported Number and DateTime Format Strings in ASP.NET AJAX

    All the best,
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Back to Top