Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Adding to Controls collection in Web Custom Control

Adding to Controls collection in Web Custom Control

Scheduled Pinned Locked Moved ASP.NET
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Syed Abdul Khader
    wrote on last edited by
    #1

    I am developing a web composite custom control which will contain many other controls. When I went through the documents in the web, I read that we have to add all the controls to the Controls collection of the control from which we are deriving our composite custom control. What is the need of this addition? In my case, I cannot directly add all the controls to the Controls collection because I have to add some of the controls to one of the constituting control. So I cannot add the same controls to a constituting control and its parent control. (hope this is correct). I need to expose the properties of the controls to be added in the inner control as the property of my custom control. What is the correct way of doing this?

    M 1 Reply Last reply
    0
    • S Syed Abdul Khader

      I am developing a web composite custom control which will contain many other controls. When I went through the documents in the web, I read that we have to add all the controls to the Controls collection of the control from which we are deriving our composite custom control. What is the need of this addition? In my case, I cannot directly add all the controls to the Controls collection because I have to add some of the controls to one of the constituting control. So I cannot add the same controls to a constituting control and its parent control. (hope this is correct). I need to expose the properties of the controls to be added in the inner control as the property of my custom control. What is the correct way of doing this?

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, When you develop a composite control, you basically override the CreateChildControls to create the appearance of the custom control. In this method, you need to add child controls to the Controls collection to make sure that they will be rendered when the parent control is rendered. If you happen to have some server controls belong to another control, then only the container will need to be added to the Controls collection of the composite control. The sample code below is a simple composite control which implements what you may need:

      public class SimpleCompositeControl : System.Web.UI.WebControls.WebControl
      {
      private TextBox textBox = new TextBox();
      private PlaceHolder holder = new PlaceHolder();
      private RequiredFieldValidator validator = new RequiredFieldValidator();

      \[Category("Appearance"),DefaultValue("")\] 
      public string Text 
      {
      	get
      	{
      		//Make sure the child control exists.
      		EnsureChildControls();
                     
      		return textBox.Text;
      	}
      	set
      	{
      		//Make sure the child control exists.
      		EnsureChildControls();
                       
      		textBox.Text = value;
      	}
      }
                 
      protected override void CreateChildControls()
      {
          //Add the TextBox to the PlaceHolder.				
      	textBox.ID = "textBox";
      	holder.Controls.Add(textBox);
                                
          //Add the RequiredFieldValidator to the PlaceHolder.
      	validator.ControlToValidate = "textBox";
      	validator.ErrorMessage = "some error message";
      	holder.Controls.Add(validator);
                  
         //Add the PlaceHolder to the Controls collection, 
         //and you don't need to add the TextBox and Validator controls.
      	this.Controls.Add(holder);
      }
      

      }

      S 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, When you develop a composite control, you basically override the CreateChildControls to create the appearance of the custom control. In this method, you need to add child controls to the Controls collection to make sure that they will be rendered when the parent control is rendered. If you happen to have some server controls belong to another control, then only the container will need to be added to the Controls collection of the composite control. The sample code below is a simple composite control which implements what you may need:

        public class SimpleCompositeControl : System.Web.UI.WebControls.WebControl
        {
        private TextBox textBox = new TextBox();
        private PlaceHolder holder = new PlaceHolder();
        private RequiredFieldValidator validator = new RequiredFieldValidator();

        \[Category("Appearance"),DefaultValue("")\] 
        public string Text 
        {
        	get
        	{
        		//Make sure the child control exists.
        		EnsureChildControls();
                       
        		return textBox.Text;
        	}
        	set
        	{
        		//Make sure the child control exists.
        		EnsureChildControls();
                         
        		textBox.Text = value;
        	}
        }
                   
        protected override void CreateChildControls()
        {
            //Add the TextBox to the PlaceHolder.				
        	textBox.ID = "textBox";
        	holder.Controls.Add(textBox);
                                  
            //Add the RequiredFieldValidator to the PlaceHolder.
        	validator.ControlToValidate = "textBox";
        	validator.ErrorMessage = "some error message";
        	holder.Controls.Add(validator);
                    
           //Add the PlaceHolder to the Controls collection, 
           //and you don't need to add the TextBox and Validator controls.
        	this.Controls.Add(holder);
        }
        

        }

        S Offline
        S Offline
        Syed Abdul Khader
        wrote on last edited by
        #3

        Thanks for ur answer. But the same thing when I did with the Table as the top level and others as the child controls for the table with child control properties exposed as the control property, I got some unusual behaviour in the viewstate. I referred the msdn documentation for Table and found the following. From MSDN: It is important to remember that any programmatic addition or modification of table rows or cells will not persist across postbacks. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. It is important to remember that any programmatic addition or modification of table rows or cells will not persist across postbacks. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. I believe the problem is exclusive to Table class only. I have followed ur suggestion now.

        M 1 Reply Last reply
        0
        • S Syed Abdul Khader

          Thanks for ur answer. But the same thing when I did with the Table as the top level and others as the child controls for the table with child control properties exposed as the control property, I got some unusual behaviour in the viewstate. I referred the msdn documentation for Table and found the following. From MSDN: It is important to remember that any programmatic addition or modification of table rows or cells will not persist across postbacks. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. It is important to remember that any programmatic addition or modification of table rows or cells will not persist across postbacks. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. I believe the problem is exclusive to Table class only. I have followed ur suggestion now.

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          + What is the unsual behaviour of your composite control? Can you post a snippet of your sample code to demonstrate that? + Basically, the web server control Table is mainly used to populate the page layout in code at runtime, it means that you will have to dynamically add child controls to the Table control, and you'll also have to do that every time the page is loaded, otherwise they will get lost.

          S 1 Reply Last reply
          0
          • M minhpc_bk

            + What is the unsual behaviour of your composite control? Can you post a snippet of your sample code to demonstrate that? + Basically, the web server control Table is mainly used to populate the page layout in code at runtime, it means that you will have to dynamically add child controls to the Table control, and you'll also have to do that every time the page is loaded, otherwise they will get lost.

            S Offline
            S Offline
            Syed Abdul Khader
            wrote on last edited by
            #5

            I answered that happened once i tried to use table in a composite control. Now I dont have it. If I find, I ll post it. Thanks for ur answer.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups