How to set input type = "number" in a dynamic text field in C # codebehind

I have a dynamically created TextBox on 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 to a simple HTML <form> :

 <input type="number"> 

... which will then call up the numeric keypad.

How can I do this using a dynamic TextBox in code, or can I?

Is there an alternative way to dynamically input a numeric input element on my page from code that will work better? Should I use JavaScript to “hack” the control after rendering it? (I would prefer that .NET can do this if possible.)

+7
source share
3 answers

I am writing this from memory, but I think it is:

 qtybox.Attributes.Add("type", "number"); 
+9
source

For those who still come up with the same issue, 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 that it is not applicable to you, make sure you have VS2010 SP1. In this case, a simple installation of SP1 may solve the problem. Download can be found at http://www.microsoft.com/en-us/download/details.aspx?id=23691 .

+3
source

This can be done using a custom control. Here you go ...

 namespace CustomTextBoxControls { public class TextBoxWithType : TextBox { public string modifyType { get; set; } protected override void Render(System.Web.UI.HtmlTextWriter output) { if (!string.IsNullOrEmpty(modifyType)) { output.AddAttribute("type", modifyType); } base.Render(output); } } } 

Register it on the aspx page ..

 <%@ Register Namespace="CustomTextBoxControls" TagPrefix="CustomControl" Assembly="CustomTextBoxControls" %> <CustomControl:MaskedTextBoxWithType id="txtNumber" modifyType="number" runat="server"></CustomControl:MaskedTextBoxWithType> 

The type attribute will be taken from the modifyType property above. Thus, it can also be a currency or any other type with HTML5 support.

0
source

All Articles