locked
Embed HTML in CreateChildControls RRS feed

  • Question

  • Hi:
     
    I am struggling a bit with getting the format the way I want on web parts in c#.  Have the logic working but can't seem to find the right controls., objets and methods to make it look better.

    For example:
    protected override void CreateChildControls()
    {
        Button mybutton = new Button();
        this.Controls.Add(mybuuton);


    }
    If I wanted to force a break (<br>) and or a horizontal line (<hr>) how would you get it to work in this method and to put a break and horizontial line after the button is added?

    Thank You

    Chuck
    • Moved by nobugz Sunday, September 21, 2008 2:55 PM forums.asp.net (Moved from .NET Base Class Library to Off-Topic Posts (Do Not Post Here))
    Saturday, September 20, 2008 7:37 PM

Answers

All replies

  • You should probably ask questions like this in the asp.net forum.  Here a two ways you can do this.


    You can just use the HtmlGenericControl to add the html

            protected override void CreateChildControls()
            {
                Button mybutton = new Button();
                this.Controls.Add(new HtmlGenericControl("br"));

                HtmlGenericControl hr = new HtmlGenericControl("hr");
                hr.Style["width"] = "1px";
                this.Controls.Add(hr);
                this.Controls.Add(mybuuton);

                PlaceHolder holder = new PlaceHolder();
                holder.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(RenderStuff));
            }




    or you could use a placeholder control and then render whatever html using htmltextwriter in RenderMethod.

            protected override void CreateChildControls()
            {
                Button mybutton = new Button();
                this.Controls.Add(mybuuton);

                PlaceHolder holder = new PlaceHolder();
                holder.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(RenderStuff));
                this.Controls.Add(holder);

            }
            private void RenderStuff(HtmlTextWriter writer, Control c)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Br);
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:10px");
                writer.RenderBeginTag(HtmlTextWriterTag.Hr);
                writer.RenderEndTag();
            }


    Sunday, September 21, 2008 6:31 AM
  • Thank you.  I was not usre where to post this since I am doing web parts.  That really helps, thanks again.

    Best Regards

    Chuck
    Sunday, September 21, 2008 7:05 PM
  • Thank you.  I was not usre where to post this since I am doing web parts.  That really helps, thanks again.

    Best Regards

    Chuck
    Sunday, September 21, 2008 7:05 PM
  •  

    Hello Chuck, 

    Thank you for your post!  I would suggest posting your question in one of the 

    HomeASP.NET Forums » .NET Languages » C#

     located here:  http://forums.asp.net/37.aspx.

    Have a great day!

    Friday, October 24, 2008 7:23 AM