asp.net-mvc 如何在 ASP.NET MVC 中设置 HTML Helper TextBox 的宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/324649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:10:52  来源:igfitidea点击:

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

asp.net-mvc

提问by Jedidja

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

我发现一些显然与旧版本 mvc 一起使用的示例表明存在各种长度参数:

<%=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.

这在当前版本中如何工作?传入样式似乎没有任何效果。

回答by Bob Palmer

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.

将为您提供一个文本框,其中包含“{ style="width:50px" }” 作为其文本内容。

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):

要针对当前版本的 MVC 1.0 进行调整,请使用以下脚本(请注意添加了第二个参数 - 我的开始为空白,请根据您的需要进行相应调整):

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

回答by Nikita Ignatov

For this you have to use HtmlAttributes, but there is a catch: HtmlAttributes and css class .

为此,您必须使用HtmlAttributes,但有一个问题:HtmlAttributes 和 css class

you can define it like this:

你可以这样定义它:

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

MVC 1

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

MVC 2

MVC 2

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

MVC 3

MVC 3

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

回答by Tim Scott

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>

回答by deleted

I -strongly- recommend build your HtmlHelpers. I think that apsx code will be more readable, reliable and durable.

我 - 强烈 - 建议构建您的 HtmlHelpers。我认为 apsx 代码将更具可读性、可靠性和耐用性。

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

按照链接; http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.aspx

回答by Developer

My real sample for MVC 3 is here:

我的 MVC 3 真实示例在这里:

<% 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 following

所以,我们有以下

"datepicker1"- ID of the control

“datepicker1”- 控件的 ID

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

DateTime.Now.ToString("MM/dd/yyyy") - 默认值

style = "width:80px;" - width of the textbox

style = "width:80px;" - 文本框的宽度

maxlength = 10- we can input 10 characters only

maxlength = 10- 我们只能输入 10 个字符

回答by Drejc

Don't use the length parameter as it will not work with all browsers. The best way is to set a style on the input tag.

不要使用长度参数,因为它不适用于所有浏览器。最好的方法是在输入标签上设置样式。

<input style="width:100px" />

回答by Roger

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

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

should be

应该

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

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

回答by Manoj

Set textbox with and maxlength in mvc3.0

在 mvc3.0 中设置文本框和 maxlength

@Html.TextBoxFor(model => model.SearchUrl, new { style = "width:650px;",maxlength = 250 })