Render ASP.NET TextBox as HTML5 Input type “Number”

后端 未结 7 1144
暗喜
暗喜 2020-11-30 12:16

When an ASP.NET TextBox renders it produces:


However I need it to render as a HTML5 number type instead, like

7条回答
  •  醉梦人生
    2020-11-30 12:33

    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.

提交回复
热议问题