asp.net-mvc How to change width Html.TextBox

2020-02-26 04:30发布

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)

5条回答
虎瘦雄心在
2楼-- · 2020-02-26 04:49

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" } ) %>
查看更多
唯我独甜
3楼-- · 2020-02-26 04:52

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" />
查看更多
你好瞎i
4楼-- · 2020-02-26 04:53

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.

查看更多
男人必须洒脱
5楼-- · 2020-02-26 04:58

If using the default MVC template that ships with VS. Explictly specifying a class in the html attributes of the textbox doesn't seem to override Site.css, however, an inline style does work. So:

{@style= "width: 400px;"} 

will work, whereas this wont:

{@class="myclass"}
查看更多
混吃等死
6楼-- · 2020-02-26 05:00
<%= Html.TextBox("state", null, new { @style = "width: 300px;" })%>
查看更多
登录 后发表回答