views:

4904

answers:

5

how do you change the width of a textbox in an asp.net-mvc View

i want to have these fields side by side and the state textbox much shorter width

            <p>
                <label for="city">City:</label>
                <%= Html.TextBox("city")%>
                <label for="state">State:</label>
                <%= Html.TextBox("state")%>
            </p>

EDIT:

none of the below answers seem to work for me. I noticed that in site.css i see this:

fieldset p 
{
 margin: 2px 12px 10px 10px;
}

fieldset label 
{
 display: block;
}

fieldset label.inline 
{
 display: inline;
}

legend 
{
 font-size: 1.1em;
 font-weight: 600;
 padding: 2px 4px 8px 4px;
}

input[type="text"] 
{
    width: 200px;
    border: 1px solid #CCC;
}

input[type="password"] 
{
    width: 200px;
    border: 1px solid #CCC;
}

how do i override this behavior for one field (textbox)

+11  A: 

I would use the helper signature that takes HTML attributes and assign it a CSS class. You would then use CSS to achieve the desired look and feel.

 <%= Html.TextBox( "state", null, new { @class = "small-input" } ) %>
tvanfosson
Note that "@class" needs the @ (literal) symbol, because class is a reserved word.
mgroves
@mgroves, absolutely correct. Thanks for clarifying. Also, note that I'm using null for the default value. This will keep the same behavior for the default value (comes from Model or ViewData) that the version without the extra parameters has.
tvanfosson
this doesn't seem to do anything. is that because i have <p> around it
ooo
please see updated question. it looks like some other css is overriding the answer above. how can i override this css ?
ooo
Add something like input[type=text] .small-input { width: 50px; } after the input[type=text] line in site.css.
tvanfosson
A: 

css

.yourcssclassname{width:50px;}

html

<%= Html.TextBox("city", null, new{@class="yourcssclassname"})%>

that should do it... you could also obviously send along a style attribute, but css classes are a better path to choose.

string.Empty represents the default value.

Edit: see tvanfosson's post

fixed answer so it solves the problems mentioned in the comments.

Peter
If you use null instead of string.Empty it will pull the default value from the Model or ViewData if available.
tvanfosson
shouldn't it be:<%= Html.TextBox("city", string.Empty, new{@class="yourclassname"})%>instead of:<%= Html.TextBox("city", string.Empty, new{@class="yourcssclassname"})%>
ooo
thanks for the tip tvanfosson I didn't know that! You're right about the typo in the css classname "me" :-)
Peter
A: 

You can use the TextBox helper to add any arbitrary attibutes to your textbox. For instance:


<%= Html.TextBox( "state", null, new { size = "10" } ) %>

Will render something like:


<input type="text" name="state" size="10" />
mgroves
This doesn't work for me. Does it work for anyone else? IE seems to ignore the setting.
Slack
Well, it's rendered server-side, so it shouldn't matter what browser you're using.
mgroves
+6  A: 
<%= Html.TextBox("state", null, new { @style = "width: 300px;" })%>
Jeff Widmer
Boo, inline styles.
mgroves
But if you are trying to just set the width quickly, as a one-off setting, this will work. I also think there are plenty of places where using inline styles is perfectly acceptable. But I also understand your objection to inline styles.
Jeff Widmer
the inline solution seems to be the only thing that trumps this external css
ooo
A: 

Why that come out to an error. it likes new{@class="yourclassname"} need to be modified..

zionfans