Styling HTML helpers ASP.NET MVC
Asked Answered
M

7

24

If I have an HTML helper like so:

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

How do I apply a CSS class to it? Do I have to wrap it in a span? Or do I need to somehow utilize the HtmlAttributes property of the helper?

Michelle answered 2/9, 2008 at 22:21 Comment(0)
P
38

You can pass it into the TextBox call as a parameter.

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

This line will create a text box with the value 20 and assign the class attribute with the value hello. I put the @ character in front of the class, because class is a reserved keyword. If you want to add other attributes, just separate the key/value pairs with commas.

Putscher answered 2/9, 2008 at 22:37 Comment(0)
N
14

This is how to add a class and a style on the same element...

"x" being the model passed to the view with a property of TextBoxID

@Html.TextBoxFor(x => x.TextBoxID, new { @class = "SearchBarSelect", style = "width: 20px; background-color: green;" })
Nolannolana answered 1/12, 2012 at 12:46 Comment(0)
B
4

I did some research and came across this article that seems to have a solution to your question.

Ajax Control Toolkit with ASP.NET MVC#

source: jimzimmerman

ARTICLE LINK

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

QUOTE

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'.

Brantley answered 2/9, 2008 at 23:2 Comment(0)
B
2

Use the htmlAttributes parameter with an anonymous type, like tihs:

<%=Html.TextBox("txtName","20", new { @class = "test"}) %>
Babin answered 2/9, 2008 at 22:38 Comment(0)
B
0

the helper implementation

public static class LabelExtensioncs
{
    public static MvcHtmlString Alarm(this HtmlHelper helper, string target, string text)
    {
        return MvcHtmlString.Create(string.Format("<p class='alert' style='background-color: #b8f89d;border-radius: 5px;width: 100%;'><b>{0}</b><br /><i>{1}</i></p>", target, text));
    }    
}

the usage in view section

@Html.Alarm("Title", "please unsure your card no is invisible in your authorized information")

the result enter image description here

Byssinosis answered 11/10, 2018 at 20:25 Comment(0)
W
-1

Theres no need to use span, because its not dynamic.

Css:

.testClass {
color: #1600d3;
}

View (Index):

@Html.TextBox("expression", "Text to show.", new { @class = "testClass" })

if you need dynamic options you can use for example:

CSS:

.test class{
background: #ffffff;
}

Controller (Index for test):

[HttpGet]
public ActionResult Index()
{
ViewBag.vbColor = "#000000";
return View();
}

View (Index):

<div>
<span>
@Html.TextBox("expression", "Text to show.", new 
{ @class = "testClass", @style="color: " + 
@ViewBag.vbColor })
</span>
</div>

Hope it helps.

Wastage answered 8/10, 2018 at 15:14 Comment(0)
W
-2

Is it that much more work?

Won answered 18/1, 2009 at 23:12 Comment(1)
For devloper no, but for Designer YES.Carloscarlota

© 2022 - 2024 — McMap. All rights reserved.