.Net MVC Html.TextBox tutorial with examples

Html.TextBox is an HTML helper method in ASP.NET MVC that generates a HTML input element of type "text". It provides an easy and flexible way to create and customize input text boxes in Razor views. It allows developers to set various HTML attributes such as ID, CSS class, size, default value, readonly, and max length, making it a powerful tool for building forms and user input interfaces in .NET MVC applications.

We will explore following examples:
  1. Add CSS Class to Html.Textbox
  2. Add Id to Html.Textbox
  3. Add HTML Attributes to Html.Textbox
  4. Add Required Attribute Html.Textbox
  5. Add Auto Focus to Html.Textbox
  6. Set Size of Html.Textbox
  7. Set Default value to Html.Textbox
  8. Make Html.Textbox Readonly
  9. Set Max Length of Html.Textbox

Add CSS Class to Html.Textbox

To add a CSS class to the HTML TextBox, we can use the Html.TextBox helper method and pass a new { @class = "classname" } anonymous object as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { @class = "form-control" })

Add Id to Html.Textbox

To add an ID to the HTML TextBox, we can use the Html.TextBox helper method and pass a new { id = "textboxid" } anonymous object as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { id = "firstname" })

Add HTML Attributes to Html.Textbox

To add custom HTML attributes to the HTML TextBox, we can use the Html.TextBox helper method and pass an anonymous object with the attribute names and values as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { data_bind = "value: firstName", placeholder = "Enter your first name" })

Add Required Attribute Html.Textbox

To add a Required attribute to the HTML TextBox, we can use the Html.TextBox helper method and add a data-val-required attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { data_val_required = "The First Name field is required." })
@Html.ValidationMessage("FirstName")

Add Auto Focus to Html.Textbox

To set the Auto-focus on the HTML TextBox, we can use the Html.TextBox helper method and add a autofocus attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { autofocus = "autofocus" })

Set Size of Html.Textbox

To set the size of the HTML TextBox, we can use the Html.TextBox helper method and add a size attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { size = "30" })

Set Default value to Html.Textbox

To set a default value for the HTML TextBox, we can use the Html.TextBox helper method and add a value attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", "John")

Make Html.Textbox Readonly

To make the HTML TextBox readonly, we can use the Html.TextBox helper method and add a readonly attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { @readonly = "readonly" })

Set Max Length of Html.Textbox

To set the maximum length of the HTML TextBox, we can use the Html.TextBox helper method and add a maxlength attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { maxlength = "50" })

In conclusion, the Html.TextBox method provides a flexible way to customize the HTML TextBox element in .NET MVC applications. By adding various HTML attributes, developers can easily customize the behavior and appearance of the HTML TextBox to meet their specific requirements.
 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
3 + 8 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice