2

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?

1
  • 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. Commented Aug 28, 2011 at 18:04

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?
And say I wanted to add this to a paragraph or h1, is there anyway to do that?
It's exactly the same, but you need to make your paragraph or h1 runat=server and give them an Id.
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
0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.