RadNumericTextBox With Variable Decimal Digits (not rounded)

Thread is closed for posting
4 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 02 Jun 2007 Link to this post

    Requirements

    RadCalendar for ASP .NET version


    RadControls for ASP .NET AJAX version

    RadInput 2.0.1 and later


    2008.1.415 and later
    .NET version

    2.0 and later

    Visual Studio version

    2005 and later
    Programming language

    C# / JavaScript
    Browser support

    all supported by RadCalendar for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX


     
    PROJECT DESCRIPTION
    This project demonstrates how you could set up the RadNumericTextBox to work with variable decimal digits count and not use the default control behavior to round the value if the decimal digit count is exceeded i.e. if you have set NumberFormat.DecimalDigits to 2 and enter 2.225, the value would remain 2.225 and would not be rounded as 2.23. The attached implementation works for AutoPostBack RadNumericTextBox as well as for client-side one.

    Basically the idea is to replace the onblur DOM event handler of the textbox and determine the necessary decimal digits count based on the current input value and change the appropriate property of the control on-the-fly (then execute the regular onblur handler so no functionality is lost). Note that you cannot use the public ClientEvents.OnBlur event of the numeric textbox as it is fired too late (after the value is already rounded):

    ASPX
    <script type="text/javascript"
    // The provided solution works for AutoPostBack as well as for client-side NumericTextBox 
    // 
    // Note: you cannot use the regular OnBlur client-side event of the numeric textbox 
    // as it is fired too late (after the value is already rounded) 
    // basically the idea is to check whether the entered number contains more decimal digits than the number format expects 
    // and that would be rounded. If that is the case we increase the NumberFormat.DecimalDigits count on-the-fly. 
    window.onload = function() 
        var numericTextBox = <%= RadNumericTextBox2.ClientID %> 
        var oldTextBoxBlurHandler = numericTextBox.TextBoxBlurHandler; 
        numericTextBox.TextBoxBlurHandler = function(e) 
        { 
            var currentValue = numericTextBox.GetTextBoxValue(); 
            var decimalPartStart = currentValue.indexOf(numericTextBox.NumberFormat.DecimalSeparator); 
             
            if (decimalPartStart != -1) 
            { 
                var decimalPart = currentValue.substring(decimalPartStart + 1); 
                decimalPartdecimalPart = decimalPart.replace(/0+$/, ""); 
                 
                var decimalDigits = parseInt(decimalPart.length, 10); 
                if (decimalDigits >= defaultDecimalDigits) 
                { 
                    numericTextBox.NumberFormat.DecimalDigits = decimalDigits
                } 
                else 
                { 
                    numericTextBox.NumberFormat.DecimalDigits = defaultDecimalDigits
                }    
            } 
            else 
            { 
                numericTextBox.NumberFormat.DecimalDigits = defaultDecimalDigits
            } 
             
            oldTextBoxBlurHandler.call(this, e); 
        } 
    </script> 
     
    <radi:RadNumericTextBox ID="RadNumericTextBox2" runat="server" Type="Percent" AutoPostBack="true" 
        OnTextChanged="RadNumericTextBox2_TextChanged"
    </radi:RadNumericTextBox> 


    We need to apply the equivalent logic on the server as well so that after the postback the NumberFormat.DecimalDigits count is not reset to the default value (this is needed for AutoPostBack NumericTextBox only):
    C#
    #region Auxiliary Properties 
    private static int DefaultDecimalDigits 
        get 
        { 
            return Thread.CurrentThread.CurrentCulture.NumberFormat.PercentDecimalDigits; 
        } 
     
    private int DecimalDigits 
        get 
        { 
            object o = this.ViewState["DecimalDigits"]; 
            if (o != null
            { 
                return (int)o; 
            } 
     
            return RadNumericTextBox2.Culture.NumberFormat.PercentDecimalDigits; 
        } 
        set 
        { 
            this.ViewState["DecimalDigits"] = value; 
        } 
    #endregion 
     
    protected void RadNumericTextBox2_TextChanged(object sender, EventArgs e) 
        // we need to apply the equivalent logic on the server as well  
        // so that after the postback the NumberFormat.DecimalDigits count is not reset to the default value 
        int decimalPartStart = RadNumericTextBox2.Text.IndexOf(RadNumericTextBox2.Culture.NumberFormat.NumberDecimalSeparator); 
        if (decimalPartStart != -1) 
        { 
            string decimalPart = RadNumericTextBox2.Text.Substring(decimalPartStart + 1).TrimEnd('0'); 
            int decimalPartLength = decimalPart.Length; 
     
            if (decimalPartLength >= DefaultDecimalDigits) 
            { 
                DecimalDigits = decimalPartLength; 
            } 
            else 
            { 
                DecimalDigits = DefaultDecimalDigits; 
            } 
        } 
        else 
        { 
            DecimalDigits = DefaultDecimalDigits; 
        } 
     
        Response.Write(RadNumericTextBox2.Text); 
     
    protected override void OnPreRender(EventArgs e) 
        base.OnPreRender(e); 
     
        CultureInfo ci = new CultureInfo("en-us"); 
        ci.NumberFormat.PercentDecimalDigits = DecimalDigits; 
        RadNumericTextBox2.Culture = ci; 
        Page.Controls.Add(new LiteralControl(String.Format("<script type='text/javascript'>var defaultDecimalDigits = {0}</script>", DefaultDecimalDigits))); 

    VB.NET
    Private Shared ReadOnly Property DefaultDecimalDigits() As Integer 
        Get 
            Return Thread.CurrentThread.CurrentCulture.NumberFormat.PercentDecimalDigits 
        End Get 
    End Property 
     
    Private Property DecimalDigits() As Integer 
        Get 
            Dim o As Object = Me.ViewState("DecimalDigits"
            If o <> Nothing Then 
                Return DirectCast(o, Integer
            End If 
     
            Return RadNumericTextBox2.Culture.NumberFormat.PercentDecimalDigits 
        End Get 
        Set 
            Me.ViewState("DecimalDigits") = value 
        End Set 
    End Property 
     
    Protected Sub RadNumericTextBox2_TextChanged(ByVal sender As ObjectByVal e As EventArgs) 
        ' we need to apply the equivalent logic on the server as well  
        ' so that after the postback the NumberFormat.DecimalDigits count is not reset to the default value 
        Dim decimalPartStart As Integer = RadNumericTextBox2.Text.IndexOf(RadNumericTextBox2.Culture.NumberFormat.NumberDecimalSeparator) 
        If decimalPartStart <> -1 Then 
            Dim decimalPart As String = RadNumericTextBox2.Text.Substring(decimalPartStart + 1).TrimEnd("0"C) 
            Dim decimalPartLength As Integer = decimalPart.Length 
     
            If decimalPartLength >= DefaultDecimalDigits Then 
                DecimalDigits = decimalPartLength 
            Else 
                DecimalDigits = DefaultDecimalDigits 
            End If 
        Else 
            DecimalDigits = DefaultDecimalDigits 
        End If 
     
        Response.Write(RadNumericTextBox2.Text) 
    End Sub 
     
    Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs) 
        MyBase.OnPreRender(e) 
     
        Dim ci As New CultureInfo("en-us"
        ci.NumberFormat.PercentDecimalDigits = DecimalDigits 
        RadNumericTextBox2.Culture = ci 
        Page.Controls.Add(New LiteralControl([String].Format("<script type='text/javascript'>var defaultDecimalDigits = {0}</script>", DefaultDecimalDigits))) 
    End Sub 
  2. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 30 Oct 2007 Link to this post

    Hello,

    Attached is a more generic version of the original project that achieves the desired functionality for multiple numeric textbox instances on the page.
  3. AS
    AS avatar
    15 posts
    Member since:
    Feb 2007

    Posted 12 Jun 2009 Link to this post

    Is it possible to do something similar when using a RadInputManager?  For example, my page is using a RadInputManager with a FormView as the target control.  In the form view, there is a large number of textboxes.  Without having to change all of my asp.TextBox in the FormView to RadNumericTextBox, is there a way to allow variable decimal digits? 
  4. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 17 Jun 2009 Link to this post

    Hello,

    Basically, such a behavior is also possible, with the difference that the nested controls will be standard textboxes, rather than numeric inputs. Nevertheless, since the same events are raised for textboxes as well, you can migrate the logic to this scenario as well.

    Kind regards,
    Yavor
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top