Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set input type="number" on dynamic textbox in c# codebehind

Tags:

html

c#

asp.net

I have a dynamically created TextBox in a C#/ASP.NET web page that I want to adapt to mobile browsers:

TextBox qtybox = new TextBox();  
qtybox.ID="qtybox";   
qtybox.Text = "0";  
qtybox.Width = 30;   
container.Controls.Add(qtybox);

I see that I can directly set this in a plain HTML <form>:

<input type="number">

...which will then bring up the numeric keyboard.

How can I do this with my dynamic TextBox in the codebehind, or can I?

Is there an alternate way to put a numeric input control on my page dynamically from the codebehind that would work better? Do I need to use JavaScript to "hack" the control after it renders? (I'd rather have a .NET way of doing it if possible.)

like image 436
Deverill Avatar asked Jan 12 '12 16:01

Deverill


2 Answers

I'm writing this from memory, but I think it's:

qtybox.Attributes.Add("type", "number");
like image 68
John Gibb Avatar answered Oct 23 '22 23:10

John Gibb


For anyone still coming here with the same problem, a few months after the OP opened this question Microsoft released an update that fixes the problem:

http://support.microsoft.com/kb/2533523 (see issue number 12).

For Visual Studio 2010, if you try to install it and it says it doesn't apply to you, check that you have VS2010 SP1. In that case, simply installing SP1 may solve the problem. The download can be found at http://www.microsoft.com/en-us/download/details.aspx?id=23691.

like image 36
Geeky Guy Avatar answered Oct 24 '22 00:10

Geeky Guy