This is a migrated thread and some comments may be shown as answers.

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

3 Answers 52 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sypher
Top achievements
Rank 1
Sypher asked on 24 Mar 2009, 11:00 PM
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.

3 Answers, 1 is accepted

Sort by
0
Todd Anglin
Top achievements
Rank 2
answered on 27 Mar 2009, 04:49 AM
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:

http://msdn.microsoft.com/en-us/library/bb397701.aspx

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

-Todd
0
Sypher
Top achievements
Rank 1
answered on 27 Mar 2009, 05:00 AM
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.
0
Nikolay Rusev
Telerik team
answered on 30 Mar 2009, 08:10 AM
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,
Nikolay
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
Sypher
Top achievements
Rank 1
Answers by
Todd Anglin
Top achievements
Rank 2
Sypher
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or