I am starting a new asp.net web app class (using c#). For my first lab I need to create a simple web page with a couple button. I have already made the page with the buttons and text. When you press the button it needs to change the text color using in-line css. How would I go about adding the in-line css to the button. Would I add some sort of C# code in the button to enable css? I am quite confused how to add the css to the web app. Any ideas?
-
This can all be done on the client side. Attach javascript to the button via the onclick attribute. Easier still use jquery (jquery.com) and add a click event handler to the button. The tutorials on the jquery site should show you everything you need.James Gaunt– James Gaunt2011-08-28 18:04:54 +00:00Commented Aug 28, 2011 at 18:04
Add a comment
|
2 Answers
From your question it sounds like you want to add inline css to some sort of element containing text when you click a Button?
protected void btn_Click(object sender, EventArgs e)
{
string inlineCss = "your css goes here";
//I'm assuming the text you want to apply css to is a Label with ID=label
label.Attributes.Add("style", inlineCss);
}
4 Comments
Brandon
I have that with a label named "label". Say I wanted to add this css "{background-color:#6495ed;}" How would I do this? Would I simply paste that in there and the program assumes I want to apply it to that label?
Brandon
And say I wanted to add this to a paragraph or h1, is there anyway to do that?
Kenneth Ito
It's exactly the same, but you need to make your paragraph or h1 runat=server and give them an Id.
Kenneth Ito
To the first comment about the background color. You are corrent, just paste it into the string for inlineCss. Do remove the curly brackets though. The program itself isn't assuming anything, it's doing what you tell it to do. You said label, I need you to add a "style" attribute to yourself with a value of whatever you put into inlineCss
Try this on click event
HtmlLink css = new HtmlLink();
css.Href = "css/fancyforms.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);
If needs inline
txtmyCtrl.Style.Add(HtmlTextWriterStyle.Color,"#33cc45");