Display "Disabled" when the value of RadNumericTextBox is 0

7 posts, 0 answers
  1. Rui
    Rui avatar
    9 posts
    Member since:
    Sep 2015

    Posted 08 Jun Link to this post

    Hello,

         I have a RadNumericTextBox used to accept a numeric value. Now I  got a requirement that when the user input 0, the text in the text box should be changed to a string like "Disabled" instead of the numeric value 0.

         Is there any solution? Thanks in advance.

    Best regards

    Zhang Rui

     

  2. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 08 Jun Link to this post

    Hello Zhang Rui,

    You can handle the valueChanged event and use the set_displayValue to change the display text, the internal value will still remain 0.

    Here is sample code:

    <script type="text/javascript">
        function valueChanged(textbox, args) {
            if (args.get_newValue() == 0) {
                textbox.set_displayValue("Disabled");
            }
        }
    </script>
    <telerik:RadNumericTextBox runat="server" ClientEvents-OnValueChanged="valueChanged"></telerik:RadNumericTextBox>

    This will also handle the case if the input is empty. You can modify it further like: args.get_newValue() === "0" if needed.

    Regards,
    Vasil
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rui
    Rui avatar
    9 posts
    Member since:
    Sep 2015

    Posted 12 Jun in reply to Vasil Link to this post

    Hello Vasil,

         Thanks for your help. It works, when the value changed to 0, "Disabled" displayed.

         But if I input '0' in the text box then press Enter key, or click the "Down" spin button at this time,  the display text changed back to '0' from "Disabled". It seems the reason is that there is no value-change event to change the display text. How can I solve it?

    Best regards

    Zhang Rui

  5. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 13 Jun Link to this post

    Hi Zhang Rui,

    If the enter causes PostBack either because of Default Button in the page or because of AutoPostBack=true for the NumericTextBoxControl. The display value will be formatted server side.

    In this case you can handle the PreRender event of the control to set the desired text.

    <script type="text/javascript">
    function valueChanged(textbox, args) {
        if (args.get_newValue() == 0) {
            textbox.set_displayValue("Disabled");
        }
    }
     </script>
    <telerik:RadNumericTextBox runat="server"
    OnPreRender="Unnamed_PreRender"
    ClientEvents-OnValueChanged="valueChanged">
    </telerik:RadNumericTextBox>

    C#
    protected void Unnamed_PreRender(object sender, EventArgs e)
    {
     
        RadNumericTextBox textbox = sender as RadNumericTextBox;
        if (textbox.Value == 0)
        {
            textbox.DisplayText = "disabled";
        }
    }


    Regards,
    Vasil
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  6. Rui
    Rui avatar
    9 posts
    Member since:
    Sep 2015

    Posted 13 Jun in reply to Vasil Link to this post

    Hi Vasil,

        Thanks for your fast response. I've tried your solution, it did not work.

         I do not think it is a PostBack issue. Actually I've disabled auto post back. I think the point is that the internal value still remains 0 when "Disabled" displayed. When you input 0 or click the "Down" spin button, the display text changed to 0, but the internal value did not change. Hence you can not get the value changed event on client side, consquently, the script was not invoked to change the display text.

        Is there any other solution? Thanks.

    Best regards

    Zhang Rui

     

  7. Rui
    Rui avatar
    9 posts
    Member since:
    Sep 2015

    Posted 16 Jun in reply to Rui Link to this post

    Hi Vasil,

        Now my problem is that when "Disabled" displayed, the value will be changed to 0 again when I click spin down. Is there any solution?

        Thanks.

    Best regards

    Zhang Rui

  8. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 16 Jun Link to this post

    Hello Rui,

    This does not happen with my test page after applying both server side and client side logic.

    Could you specify the version of RadControls that you are using and your browser?

    Regards,
    Vasil
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017