locked
Can I use Up and Down arrow key in a ASP.net textbox? And set min and max value? RRS feed

  • Question

  • User482190417 posted

    I have an ASP.net TextBox with Number TextMode, I'm wondering if I could use Up and Down key to increment and decrement the value, and set the min and max value in easy way. Some suggestions?

    Friday, February 19, 2021 4:43 PM

All replies

  • User409696431 posted

    Setting the TextMode to Number uses the browser's HTML5 understanding of HTML input attributes.  (It assumes you are using framework 4.5 or above.)

    Modern browsers put an up/down arrow in the resulting HTML input that lets you increment or decrement the value you've entered.  This implementation is under the browser's control, not yours.  And most browsers do, in fact, respond to the up and down keys for triggering this arrow function, as long as the textbox has focus.  (Try it out.)

    So the only thing you are left with is setting a min and max value.  Use a RangeValidator.  This won't prevent people from typing, arrowing, or up/down key entering a value that is out of range, but it will display an error message until they correct it, and the value will not be valid for submitting the form.

                <asp:TextBox ID="TextBox1" runat="server" TextMode="Number"></asp:TextBox>
                <asp:Rangevalidator ID="Rangevalidator1" errormessage="Please enter value between 10-55." forecolor="Red" controltovalidate="TextBox1" minimumvalue="10" maximumvalue="55" runat="server" Type="Integer">
                </asp:Rangevalidator>

    Sunday, February 21, 2021 7:43 PM
  • User-1545767719 posted

    I have an ASP.net TextBox with Number TextMode, I'm wondering if I could use Up and Down key to increment and decrement the value, and set the min and max value in easy way. Some suggestions?

    Why don't use the NumericUpDown in the Ajax Control Toolkit?

    NumericUpDown Demonstration
    https://ajaxcontroltoolkit.devexpress.com/NumericUpDown/NumericUpDown.aspx

    Unlike the input type="number", the NumericUpDown is NOT browser-dependent and can be used in most major browsers (including IE) with same appearance. It has the Maximum and Minimum properties you require.

    Monday, February 22, 2021 1:30 AM