Search code examples
asp.netcustom-server-controls

What is the proper way to extend <asp:Panel>?


I am attempting to create a custom server control "CollapsablePanel" that extends ASP.net's Panel. Essentially what I'm trying to do is take the current Panel, add a title bar and any necessary javascript to add the collapse/expand functionality. Other than that I want the .aspx syntax and general panel functionality to remain the same. Is there some sort of a best practice for this situation, or will I eventually have to just completely overwrite how the Panel's HTML output is rendered?

Thanks,

Mike


Solution

  • It sounds to me like you'll just want to derive a new Panel class and then override the Render functionality:

    public class MyPanel : Panel
    {    
        protected override void Render(HtmlTextWriter writer)    
        {
            writer.Write("<div id=\"" + base.ClientID + "\">");
            writer.Write("..."); // Whatever else you want to render.
            writer.Write("</div>");
        }
    }