When an ASP.NET TextBox renders it produces:
<input type="text" />
However I need it to render as a HTML5 number type instead, like:
<input type="number" />
Is this possible?
When an ASP.NET TextBox renders it produces:
<input type="text" />
However I need it to render as a HTML5 number type instead, like:
<input type="number" />
Is this possible?
I had the same requirement for a mobile website using ASP.NET. After finding no good solution, I tried simply setting
type="number"
directly on the textbox. To my surprise, it worked! Incredulous, I created a simple test project to double-check. I ran this line of code in each .NET version:Here are the results:
Bottom line: if you are using ASP.NET 4.0 or newer, just add
type="number"
to your textbox.you would have to create a new control inheriting from
TextBox
and override the rendering, or you can generate a javascript snippet to change it after the fact.You could use the members of the Enum TextBoxMode
This render
the full Enum is
I was able to do this with a dynamically created control like this:
The
TextMode
property is used for that, e.g.might be rendered as
The textmodes in addition to [ MultiLine | Password | SingleLine ] were introduced some time after VS2010 - the documentation does not quickly tell me exactly when.
Override the base textbox control
Or you can check the one I just found at http://www.codeproject.com/Articles/68834/Enhanced-Textbox-Control