Home All Groups Group Topic Archive Search About
Author
9 Dec 2005 2:22 PM
magnus
Hi,
Hoping 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

Author
9 Dec 2005 4:03 PM
agapeton
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/
Author
13 Dec 2005 1:50 PM
magnus
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.