Embedded CSS not working during designtime [modified]
-
I have an embedded css resource which contains styling info for a table. When I run my app, the table is shown with the styles specified. During design time, these styles are not applied to my table. More info below. I think my problem may be that the OnPreRender may not be run during design time. What are my options? Thanks in advance. assembly.cs: [assembly: WebResource("MyNamespace.StyleSheet.css", "text/css", PerformSubstitution=true)] MyControl.cs:
void RegisterStyleSheet() { string includeTemplate = ""; string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyNamespace.StyleSheet.css"); LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation)); ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(include); } protected override void OnPreRender(EventArgs e) { RegisterStyleSheet(); base.OnPreRender(e); }
modified on Friday, July 9, 2010 1:54 PM
-
I have an embedded css resource which contains styling info for a table. When I run my app, the table is shown with the styles specified. During design time, these styles are not applied to my table. More info below. I think my problem may be that the OnPreRender may not be run during design time. What are my options? Thanks in advance. assembly.cs: [assembly: WebResource("MyNamespace.StyleSheet.css", "text/css", PerformSubstitution=true)] MyControl.cs:
void RegisterStyleSheet() { string includeTemplate = ""; string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyNamespace.StyleSheet.css"); LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation)); ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(include); } protected override void OnPreRender(EventArgs e) { RegisterStyleSheet(); base.OnPreRender(e); }
modified on Friday, July 9, 2010 1:54 PM
Figured it out. I changd the RegisterStyleSheet to return a string:
string RegisterStyleSheet() { string includeTemplate = ""; string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Fyuzon.Membership.Web.Css.BaseGridStyleSheet.css"); return String.Format(includeTemplate, includeLocation); }
and override the RenderControl with the following:
public override void RenderControl(HtmlTextWriter writer) { writer.Write(this.RegisterStyleSheet()); base.RenderControl(writer); }
Works like a charm... If there is a better way of doing this, please do let me know.