I need to add a HTML style container in the section of a partic开发者_如何学JAVAular page, like so:
<style>
#mycontrol
{
color:#ff0000;
}
</style>
Although there are quite a few ways of doing this I was thinking about instantiating a HtmlControl from the System.Web.UI.HtmlControls namespace and simply render it on the page. However I only found the HtmlGenericControl to be the closest candidate - is there a more suitable control I can use or do I have to use another approach?
Try something like
HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "body{background-color:#000000;}";
Page.Header.Controls.Add(style);
HTH
Ivo Stoykov
you can try this:
var myStyle =
new Style { ForeColor = System.Drawing.Color.FromArgb(255, 0, 0) };
Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");
You can use an HtmlGenericControl - or you can use a Literal if you want to.
A better way might be to insert this into your HTML Header using something like this code from http://msdn.microsoft.com/en-us/library/system.web.ui.page.header.aspx
protected void Page_Load(object sender, System.EventArgs e)
{
// Create a Style object for the body of the page.
Style bodyStyle = new Style();
bodyStyle.ForeColor = System.Drawing.Color.Blue;
bodyStyle.BackColor = System.Drawing.Color.LightGray;
// Add the style rule named bodyStyle to the header
// of the current page. The rule is for the body HTML element.
Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "body");
// Add the page title to the header element.
Page.Header.Title = "HtmlHead Example";
}
精彩评论