Logo 
Search:

Sharepoint FAQ

Submit Interview FAQ
Home » Interview FAQ » SharepointRSS Feeds

What is the CreateChildControls() method? How can you use it to do something simple like displa

  Shared By: Tarang Nayak    Date: Mar 12    Category: Sharepoint    Views: 948

Answer:

What is the CreateChildControls() method? How can you use it to do something simple like displaying a Label control?


The CreateChildControls method in WebParts is used to notify the WebPart that there are children controls that should be output for rendering. Basically, it will add any child ASP.NET controls that are called instantiating each control with its relevant properties set, wire any relevant event handlers to the control, etc. Then the add method of the control class will add the control to the controls collection. In the relevant WebPart render method, the EnsureChildControls method can be called (or set to false if no child controls should be called) to ensure that the CreateChildControls method is run. When using CreateChildControls it implies that your WebPart contains a composition of child controls.

In order to create something like a label control in Create, you would create a new label control using the new keyword, set the various properties of the control like Visible=True and ForeColor = Color.Red, and then use Controls.Add(myLabelControl) to add the control to the controls collection. Then you can declare EnsureChildControls in the Render method of the WebPart.

Share: 
 



Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: