asp.net-mvc 如何改变宽度 Html.TextBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1063737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net-mvc How to change width Html.TextBox
提问by leora
how do you change the width of a textbox in an asp.net-mvc View
如何在 asp.net-mvc 视图中更改文本框的宽度
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:
以下答案似乎都不适合我。我注意到在 site.css 中我看到了这个:
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)
我如何覆盖一个字段(文本框)的这种行为
回答by tvanfosson
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 属性的帮助程序签名并为其分配一个 CSS 类。然后,您将使用 CSS 来实现所需的外观和感觉。
<%= Html.TextBox( "state", null, new { @class = "small-input" } ) %>
回答by Jeff Widmer
<%= Html.TextBox("state", null, new { @style = "width: 300px;" })%>
回答by Peter
css
css
.yourcssclassname{width:50px;}
html
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.
应该这样做......你显然也可以发送一个样式属性,但是css类是一个更好的选择路径。
string.Empty represents the default value.
string.Empty 表示默认值。
Edit: see tvanfosson's post
编辑:见 tvanfosson 的帖子
fixed answer so it solves the problems mentioned in the comments.
固定答案,因此它解决了评论中提到的问题。
回答by Matthew Groves
You can use the TextBox helper to add any arbitrary attibutes to your textbox. For instance:
您可以使用 TextBox 助手将任意属性添加到您的文本框。例如:
<%= Html.TextBox( "state", null, new { size = "10" } ) %>
Will render something like:
将呈现如下内容:
<input type="text" name="state" size="10" />
回答by bcleary
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:
如果使用 VS 附带的默认 MVC 模板。在文本框的 html 属性中明确指定一个类似乎不会覆盖 Site.css,但是,内联样式确实有效。所以:
{@style= "width: 400px;"}
will work, whereas this wont:
会起作用,而这不会:
{@class="myclass"}

