views:

266

answers:

3

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

A: 

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...
}
cnobles
Thanks for the response! I've already got that basic groundwork setup for the control, and I am even using the control in my aspx page. Of course, I haven't don't anything else to the control other than extend Panel so it doesn't do anything different :). Ultimately I imagine that where Panel might render something like this:<div id="myPanel">Panel Content</div>I'd like it to render something like this:<div id="myPanel"> <div class="PanelTitle">My Panel Title</div> <div class="PanelContent">My Panel Content</div></div>and any controls/scripts required to expand or collapse.
Mike C
Obviously, I would like to be able to set 'My Panel Title' to some property value 'Title' that the user sets and set 'My Panel Content' to whatever comes between the <webstation:CollapsablePanel></webstation:CollapsablePanel> tags.
Mike C
If that's the case, I would recommend you take a look at the original panel's render code, as you're going to have to override it to get the desired result.
cnobles
+2  A: 

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:

Jeff Sternal
Jeff,Yes I've seen the CollapsiblePanel extender in the ToolKit, but it doesn't quite suit my needs. Plus this is an excellent opportunity to learn how to develop my own controls! :)
Mike C
I hear you, I did the same thing. :)
Jeff Sternal
+1  A: 

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>");
    }
}
CAbbott
CAbbott, That is likely the route I will take to get this done, I just wasn't sure if it was better to somehow append to the existing Panel HTML rather than writing it all by hand. Luckily it's not much work to recreate the Panel HTML ;). One question I have though is how to I access the contents of the CollapsiblePanel tag on the .aspx page. For example, if my aspx says <uc:CollapsiblePanel>MY CONTENT</uc:CollapsiblePanel> how do I access MY CONTENT inside the Render method?Thanks.
Mike C
I believe that you'd have to look through any controls that are contained within the panel and render them as so: foreach (Control cur in Controls) cur.RenderControl(writer)
CAbbott
I got away with it by using "Me.RenderChildren(writer)". Do you know if that will handle recursively rendering any controls within my child controls?
Mike C