Hi, I'm currently writing a custom server control in ASP.net. My idea is to create some sort of collapsible panel control (btw, it's the first server control I'm writing). So what I wanted to have is that the user defines my control in this way in the page:
<myCtrl:CollapsiblePanel id="myCollapsiblePanel" runat="server">
<asp:Label id="lblTest" runat="server" Text="Test Label:"/>
<asp:TextBox id="txbTest" runat="server"/>
</myCtrl:CollapsiblePanel>
The control should then render on the final page as follows:
<div id="myCollapsiblepanel">
<input type="submit" name="btnExpand" value="Expand/Collapse" id="btnExpand" />
<div id="pnlContent">
<span id="lblTest">Title:</span>
<input name="txbTest" type="text" id="txbTest" />
</div>
</div>
The submit button "btnExpand" has then an event-handler on the control which hides/shows the panel "pnlContent". My first approach was to create a server control that inherits from Panel. Inside the CollapsiblePanel control I then created another Panel "pnlContent". In the OnInit I did something like the following:
protected override void OnInit(EventArgs e)
{
for (int i = 0; i < base.Controls.Count; i++)
{
Control control = base.Controls[i];
content.Controls.Add(control);
}
base.Controls.Clear();
base.Controls.Add(button);
base.Controls.Add(content);
}
I basically took all the controls which are currently present inside my control and add them to the control list of the internally created Panel pnlContent. Furthermore I added the button which then does the expanding/collapsing of then pnlContent. My problem is that somehow the rendering doesn't work correctly. The button and the pnlContent are rendered out correctly, just the controls inside the pnlContent are not shown. The output is like the following
<div id="myCollapsiblepanel">
<input type="submit" name="btnExpand" value="Expand/Collapse" id="btnExpand" />
<div id="pnlContent"> </div>
</div>
The pnlContent is empty... Could someone suggest me a solution for this and/or what I did wrong with the rendering. I didn't override the default rendering of the Panel I inherit since I thought this shouldn't be necessary because I modify its control structure in the OnInit, so it should render out my modifications..
For the moment I found a solution. I basically did it the same way it is most often done in the Microsoft Ajax Control toolkit. I created a socalled "Extender" which doesn't render itself but just adds behavior. So now I add the the id of the panel to collapse to my CollapsiblePanelExtender control and it does then the job of collapsing/expanding. Maybe it's also the better and cleaner solution but I still would like to know what went wrong in my first approach I described before.
thanks for any help!