Adding to Controls collection in Web Custom Control
-
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?
-
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?
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 theControls
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 theControls
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); }
}
-
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 theControls
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 theControls
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); }
}
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.
-
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.
+ 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.
-
+ 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.
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.