样式化HTML帮助器ASP.NET MVC

时间:2020-03-05 18:46:47  来源:igfitidea点击:

如果我有这样的HTML帮助器:

Name:<br />
<%=Html.TextBox("txtName",20) %><br />

如何将CSS类应用于它?我需要把它包起来吗?还是我需要以某种方式利用帮助程序的HtmlAttributes属性?

解决方案

回答

我们可以将其作为参数传递到TextBox调用中。

Name:<br/>    
<%= Html.TextBox("txtName", "20", new { @class = "hello" }) %>

该行将创建一个值为20的文本框,并为class属性分配值为hello。我将@字符放在类的前面,因为class是一个保留关键字。如果要添加其他属性,只需用逗号分隔键/值对。

回答

使用带有匿名类型的htmlAttributes参数,例如tihs:

<%=Html.TextBox("txtName","20", new { @class = "test"}) %>

回答

我做了一些研究,发现这篇文章似乎可以解决问题。

带有ASP.NET MVC#的Ajax控件工具包

资料来源:jimzimmerman

文章链接

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330

引用

So basically if you put the class name
  TextboxWatermark on any textbox input
  with the title you like to show as the
  watermark like this:

<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />

  
  or

<%= Html.TextBox("username", new { @class = "TextboxWatermark", @title = "Must be at least 6 chars" }) %>

  
  What is nice about the second option
  is that you get the added benefit of
  getting the View Engine to fill out
  the value of the textbox if there is
  an item in ViewData of the
  ViewData.Model that has a var named
  'username'.

回答

还有更多工作吗?