|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CSS class for a div tagHoping someone can help me. I´m building a composite control and there is one thing I haven't figured out. What I want to do is to set a css class, specified in the properties, onto a div tag, that is rendered as LiteralControl (in the CreateChildControls() method). This div tag surrounds my control and is ment to set the base css class for it (then I have sub css classes for the content) I cant get to the css property in this method, as I need to do that in OnPreRender(...) Controls.Add( new LiteralControl( "<div id=\"baseTag\" class=\"???\"" ) ); // ... some other contols Controls.Add( new LiteralControl( "</div>" ) ); One way to add div tags is to instanciate a panel, and then setting the CssClass for it in OnPreRender. But then I would not have the closing div tag at the end. Any help would be appreciated, thanks, Magnus I admire your use of proper web code. You should feel good that you
are one of the only ASP.NET developers in the world who should still have a job. I am however confused by your request... Why are you worried about OnPreRender? I have an extremely complex control that I use in a few applications and I always create the xhtml objects using dom-like methods. For example... HtmlGenericControl _div = new HtmlGenericControl("div"); if (ShowBorder) { _div.Attributes.Add("class", this.BlockCssClass); } else { _div.Attributes.Add("class", this.BlockCssClass + " SetupBlock"); } HtmlGenericControl _footerDiv = new HtmlGenericControl("div"); .... _footerDiv.Attributes.Add("class", "BlockFooter"); .... then I have a ton of other stuff all adding more children. If I get an exception at any point I do something like this... catch (Exception ex) { Literal _error = new Literal( ); if (ex.Message == "Invalid URI: The URI scheme is not valid") { _error.Text = "<span class=\"ErrorMessage\">Incompatible feed detected.</a>"; } else { _error.Text = "<span class=\"ErrorMessage\">" + ex.Message + "</a>"; } _div.Controls.Add(_error); } .... on and on and on... _div.Controls.Add(_h3); and finally... _div.Controls.Add(_footerDiv); this.Controls.Add(_div); base.CreateChildControls( ); That's just an example... nothing there is complete here as I copy/pasted from one of my own controls. David Betz http://www.davidbetz.net/dynamicbliss/ http://www.davidbetz.net/winfx/ Thanks for taking the time to answer and thanks for your compliment ;)
however I am new at asp.net, having been programming win-forms for a while. Your answer led me to the solution, thanks. And for future ref. here is the solution: Instead of adding the div as an LiteralControl I add it as an HtmlGenericControl, and then add all the rest of the contols to that control. protected override void CreateChildControls() { baseDiv = new HtmlGenericControl( "div" ); Controls.Add( baseDiv ); baseDiv.Controls.Add( ... rest of the controls ) } This allows me to access it in the OnPreRender method: protected override void OnPreRender( EventArgs e ) { baseDiv.Attributes.Add( "class", this.CssClass ); ... } thanks.
Understanding query strings
contenteditable Referencing controls in a template programmatically Control.Controls bug? Control's child controls missing at the run time. Master Detail w Multiple Keys Tree View DragNode onload called only once preserve password field over steps in wizard validation controls are not working Events not fired for Dynamically Created Controls in VS2005 ASP.NET 2.0 |
|||||||||||||||||||||||