views:

3051

answers:

5

I've been trying to create a custom control that works exactly like the Panel control except surrounded by a few divs and such to create a rounded box look. I haven't been able to find a decent example of how to do this.

I need to be able to place text and controls inside the control and access it directly without referencing the panel (exactly the way the Panel control works).

Does anyone have any examples of this?

+7  A: 

There is two ways to do this. One is to implement INamingContainer on your control, and it takes a lot of effort.

The other way is to inherit from Panel, and override the RenderBeginTag and RenderEndTag methods to add your custom markup. This is easy.

public class RoundedCornersPanel : System.Web.UI.WebControls.Panel
{
    public override RenderBeginTag (HtmlTextWriter writer)
    {
        writer.Write("Your rounded corner opening markup");
        base.RenderBeginTag(writer);
    }

    public override RenderEndTag (HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);
        writer.Write("Your rounded corner closing markup");                     
    }
}
FlySwat
This is probably a very stupid question but since I can't place this code in a typical user control (.ascx), where would I put it? I created a class and placed it in there, but then I don't know how to add it to a page (dragging just creates a link)
Arthur Chaparyan
Post a question on how to use ASP.NET Server controls, and I can answer, I can't fit the explanation into this little comment box.
FlySwat
If you build the code it will appear on the toolbox.
Bruno Shine
Thanks Jonathan, I really appreciate your help:http://stackoverflow.com/questions/306381/aspnet-custom-controls
Arthur Chaparyan
+1  A: 

Create a class that inherits System.Web.UI.Control, and overrride the Render ( HtmlTextWriter ) method. In this method, render surrounding start tags, then render the children(RenderChildren), then render end tags.

protected override void Render ( HtmlTextWriter output )
{
  output.Write ( "<div>" );
  RenderChildren ( output );
  output.Write ( "</div>" );
}

Rounded corners is typically achieved using CSS and corner images for the top left, top right, bottom left and bottom right corners. It could be done using 4 nested divs, acting as layers, each of them having one corner image as their background image.

baretta
A: 
Daok
A: 
public class myCustomPanel : Panel
{
    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "top_left_corner");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.RenderBeginTag(writer);
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
            base.RenderEndTag(writer);
        writer.RenderEndTag();
    }

}
Bruno Shine
A: 

Just another thing you can use, there's a rounded corner extender in the ASP.Net ajax toolkit.

I know it's not exactly what you asked for, but you don't have to write any custom code.

Hope that helps!

Zachary Yates