How to set width of TextBox HTML helper in ASP.NET MVC?

Some examples that I found to seem to work with older versions of mvc suggest that there is a sort parameter of length:

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

But that could erroneously set the meaning.

How does it work in the current version? Walking in style has no effect.

+45
asp.net-mvc
Nov 27 '08 at 21:06
source share
8 answers

The original answer no longer works as it is written:

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

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

To adjust this for the current version of MVC 1.0, use the following script (note the addition of the second parameter - I have an initial start-up, adjust it according to your needs):

 <%=Html.TextBox("test", "", new { style="width:50px" })%> 
+62
Jan 23 '10 at 16:47
source share

You need to use HtmlAttributes for this , but there is catch: HtmlAttributes and css class .

you can define it as follows:

 new { Attrubute="Value", AttributeTwo = IntegerValue, @class="className" }; 

and here is a more realistic example:

 new { style="width:50px" }; new { style="width:50px", maxsize = 50 }; new {size=30, @class="required"} 

and finally in:

MVC 1

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

MVC 2

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

MVC 3

 @Html.TextBox("test", null, new { style="width:50px" }) 
+40
Nov 28 '08 at 4:58
source share

Something like this should work:

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

Or better:

 <%=Html.TextBox("test")%> <style type="text/css"> input[type="text"] { width:50px; } </style> 
+6
Nov 27 '08 at 22:28
source share

I highly recommend building your own HtmlHelpers. I think apsx code will be more readable, reliable and durable.

Follow the link; http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.aspx

+2
05 Oct 2018-10-10 at
source share

Do not use the length parameter as it will not work with all browsers. The best way is to set the style for the input tag.

 <input style="width:100px" /> 
+1
Nov 27 '08 at 22:56
source share

My real sample for MVC 3 is here:

 <% using (Html.BeginForm()) { %> <p> Start Date: <%: Html.TextBox("datepicker1", DateTime.Now.ToString("MM/dd/yyyy"), new { style = "width:80px;", maxlength = 10 })%> End Date: <%: Html.TextBox("datepicker2", DateTime.Now.ToString("MM/dd/yyyy"), new { style = "width:80px;", maxlength = 10 })%> <input type="submit" name="btnSubmit" value="Search" /> </p> <% } %> 

So we have the following

"datepicker1" - control identifier

DateTime.Now.ToString ("MM / dd / yyyy" ) - the default value

style = "width: 80px; " - the width of the text field

maxlength = 10 - we can enter only 10 characters

+1
Apr 12 2018-11-11T00:
source share

new {style = "width: 50px", maxsize = 50};

it should be

new {style = "width: 50px", maxlength = 50};

0
Nov 15 '09 at 9:23
source share

Set textbox with maxlength to mvc3.0

 @Html.TextBoxFor(model => model.SearchUrl, new { style = "width:650px;",maxlength = 250 }) 
0
Jan 07 '13 at 6:41
source share



All Articles