How to Programmatically Apply a CSS Class to an ASP.NET Control

How do you programmatically apply a css class to an asp.net control?

You can just do this:

 firstName.CssClass = "input left selected".

If you want to append to any existing class names, do this:

firstName.CssClass += " selected";

Add additional Css class programmatically

You can set the CssClass property of the ASP.NET Textbox control. To add more than one CSS class for an element, just separate it with a space:

MyTextBox.CssClass = "class1 class2";

You can put this in your OnClick event handler:

<asp:TextBox ID="MyTextBox" runat="server" OnClick="MyTextBox_Click" />

Then in code-behind:

void MyTextBox_Click(Object sender, EventArgs e) {
MyTextBox.CssClass = "class1 class2";
}

ASP.NET: Create new CSS class programmatically from User Control?

If the CSS is defined in your .ASCX file, it should be rendered every time, postback or not. Unless the control itself is set to Visible="false".

One workaround is to define the CSS code within an asp:Literal block on your control. Your control can then expose a public function that simply returns the contents of that literal to the caller. If a host is going to make your control invisible, they can grab the CSS code using that public function, and place it within the head section of the page. In that way, the CSS definition should always be there, regardless of the Visibility setting of the control.

In the larger scheme of things, Adam is correct: it's better to keep your CSS code in .CSS files wherever possible.

programmatically apply style to an asp.net website

The <style> tag can also be used as server control:

<style type="text/css" runat="server" id="htmlCss"></style>

This will generate a field of type HtmlGenericControl in the page.

In one of the page life-cycle events (Page_Load, Page_Init, etc), assign the literal CSS definition like this:

var css = @"
body
{
background-color:#b0c4de;
}";
htmlCss.InnerHtml = css;

ASP.NET: Creating a CSS class programmatically

First apply a css class to you control and Crete style sheet in page header as below

//// Create dynamic style rule which applies to the CSS class selector (".MyCssClass")
Style dynamicClassStyle = new Style();
dynamicClassStyle.BorderStyle = BorderStyle.Solid;
dynamicClassStyle.BorderColor = System.Drawing.Color.Black;
dynamicClassStyle.BorderWidth = new Unit(1);
dynamicClassStyle.BackColor = System.Drawing.Color.White;
Page.Header.StyleSheet.CreateStyleRule(dynamicClassStyle, null, ".MyCssClass");

Full article here

assign css class to a panel from code asp.net c#

You can change the CssClass property of the Panel to set the class, as shown in this answer How do you programmatically apply a css class to an asp.net control?

You can just do this:

 firstName.CssClass = "input left selected". 

If you want to append to any existing class names, do this:

 firstName.CssClass += " selected";

How to add an additional css class from code behind using ASP.NET?

Add additional CssClass like this:

txt.CssClass = txt.CssClass + " Error"

The above can also be abbraviated as:

txt.CssClass += " Error"


Related Topics



Leave a reply



Submit