0

I have a custom control which uses System.Windows.Form.Textbox as the underlying text box.

I want to change that type to type="number" however I don't see a way to do this.

The end goal is to have an input field which, when clicked on a smartphone browser, will give you only numeric keys to enter rather than the full keyboard.

It is possible to restrict the input to numbers only using JavaScript, but that is not what I want. I specifically need the keyboard to open with only numbers on it. If there is an alternative way to achieve this than setting type="number" then that should work as well.

rbento
  • 9,919
  • 3
  • 61
  • 61
Final Hazard
  • 77
  • 1
  • 7
  • Maybe create a number only textbox as mentioned in this SO question: http://stackoverflow.com/questions/10758683/how-to-force-only-numeric-values-be-allowed-in-maskedtextbox Specifically the very last comment – Ash May 18 '16 at 02:16
  • @ashwin Are you suggesting to use NumericUpDown instead? Does that control generate gtml for input type="number"? – Final Hazard May 18 '16 at 20:38
  • A few suggestions here: http://stackoverflow.com/questions/9398356/render-asp-net-textbox-as-html5-input-type-number – johna May 19 '16 at 02:00

1 Answers1

0

You can add type="number" to an ASP.NET TextBox and it will render with that attribute.

<asp:TextBox ID="TextBox1" runat="server" type="number" />

Depending on ASP.NET version it will either render with both type="text" and type="number" (v2.0 - v3.5) or just type="number" (v4.0+).

If you wanted to add this conditionally in your code-behind, you could:

TextBox1.Attributes.Add("type", "number");
johna
  • 10,540
  • 14
  • 47
  • 72