Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:29:08+00:00 2026-05-11T11:29:08+00:00

I’m currently writing a custom server control in ASP.net. My idea is to create

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:29:08+00:00Added an answer on May 11, 2026 at 11:29 am

    This happens because at any given moment a control can only belong to one parent. When you add a control from one collection into another, it is removed from the first one automatically. In your case:

    for (int i = 0; i < Controls.Count; i++) {    //obtain a reference    Control control = Controls[i];    //The control is removed from Controls collection     //and added to content.Controls. All remaining controls get shifted left.    //This means that next control will get skipped.    content.Controls.Add(control); } 

    Consider doing following (by the way, you probably don’t need to use base in this case):

    Control[] controls = new Control[Controls.Count]; Controls.CopyTo(controls, 0); //create a snapshot of controls  for (int i = 0; i < controls.Length; i++)     content.Controls.Add(controls[i]); 

    or use reverse addition:

    Control[] controls = new Control[Controls.Count]; for (int i = Controls.Count - 1; i >= 0; i--) //walk backwards     controls[i] = Controls[i];  for (int i = 0; i < controls.Length; i++)     content.Controls.Add(controls[i]); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.