HTML and CSS Reference
In-Depth Information
Figure 5-10.
The
TextMode
property supports the new input types.
At runtime, the ASP.NET framework converts the
TextMode
setting to the appropriate input type
attribute. Note that you can also set the
type
attribute directly in the
<asp:TextBox>
markup tag to get the
same effect. However, the recommended way is to set the
TextMode
property. Of course, nothing prevents
you from using a plain HTML5
<input>
tag with the required setting for its
type
attribute. But if you wish to
access the text box from the server-side code, then chances are you should use the
TextBox
server control.
If you wish to set attributes such as
min
,
max
, and
step
, you need add them directly to the
<asp:TextBox>
markup tag as shown here:
<asp:TextBox ID="TextBox1" runat="server" TextMode="Number"
min="18" max="100" step="2">
</asp:TextBox>
n
Note
If you're using Visual Studio 2010, you may need to install the latest Service Pack to be able to use the
new input types.
Using the New Input Types in MVC Views
In MVC views, things are bit easier when it comes to using the new HTML5 input types. Because MVC
views rely directly on the HTML markup rather than server controls, you can easily use
<input>
tags as
discussed earlier. If you prefer to use HTML helpers instead of raw HTML markup, you can use the new
input types as follows:
<%= Html.TextBox("txtNumber","18",new {type="number",min="18",max="100"}) %>
As you can see, the
TextBox
helper takes three parameters: the name of the resulting
<input>
element,
its default value, and an object representing additional HTML attributes that need to be added to the
element. In this example, you set the
type
,
min
, and
max
attributes.