What is the proper way to extend <asp:Panel>?
I am attempting to create a cus开发者_StackOverflow社区tom 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
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>");
}
}
The Asp.Net AJAX Control Toolkit includes a class that sounds like it might suit your needs: the CollapsiblePanel
.
If that doesn't do the trick, you might consider inheriting from CompositeControl
rather than directly from Panel
since it sounds like you'll want your control to include a Label
, an Image
, and a Panel
at a minimum.
One advantage of inheriting from CompositeControl
is that even though you'll have to implement some of the rendering logic yourself, you can delegate most of the actual HTML rendering to the child controls. Some useful links:
- Developing a Composite Control
- A Crash Course on ASP.NET Control Development: Building Composite Controls
You don't give much detail about how you want to extend, past the basic concept, but all you need to do is just make your own, derived from Panel as the base class.
class MyPanel : Panel
{
public MyPanel()
{
}
etc...
}
精彩评论