Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the width of an HTML Helper TextBox in ASP.NET MVC?

Tags:

asp.net-mvc

Some examples I found that apparently worked with older versions of mvc suggest that there was a length parameter of sorts:

<%=Html.TextBox("test", 50)%> 

But that may have been mistakenly setting the value.

How do this work in the current release? Passing in the style doesn't appear to have any effect.

like image 372
Jedidja Avatar asked Nov 27 '08 21:11

Jedidja


People also ask

How can create HTML helper in ASP.NET MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What is the role of HTML helper in ASP.NET MVC?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.

What is templated HTML helpers in MVC?

Template helper is introduced in MVC-2, and it is classified into two categories. @Html. DisplayForModel() :- This Display template helper is used with strongly typed views. It goes through the every property in the model for displaying object.

What is strongly typed HTML helpers in MVC?

The Strongly-Typed HTML helper (i.e., NumericTextBox) takes lambda as a parameter that tells the helper, which element of the model to be used in the typed view. The Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure.


1 Answers

The original answer is no longer working as written:

<%=Html.TextBox("test", new { style="width:50px" })%>  

will get you a text box with "{ style="width:50px" }" as its text content.

To adjust this for the current release of MVC 1.0, use the following script (note the addition of the second parameter - I have mine starting as blank, adjust accordingly based on your needs):

<%=Html.TextBox("test", "", new { style="width:50px" })%>  
like image 152
Bob Palmer Avatar answered Sep 28 '22 03:09

Bob Palmer