Composite Custom Controls are those
controls which contain other controls. It is almost like a custom user control in ASP.NET which is used like
a a container to keep another control in it. Here we are going to discuss about
the composite custom control, how can we make it and used them later inside a web
application. For this we have to inherit the Composite Control Class, It is very
useful class we can use it's functionality or features which becomes very
helpful to us. In the Composite Control you will have to create the instance of
the control for which control you want to add into the container. Let we move
forward that how will we create and use later, Now follow some step which is
Step 1: Firstly we are going to open the
Web server Control application.
Step 2: Now you have to
change the name of the ServerControl.cs file to MC_Control.cs.
Step 3: Further
Toolbox Data attribute for the class, change the
string "ServerCOntrol1" to "MC_Control" in both places where it occurs. let see
how we will add it.
Step 4: Now write code for
the class named as MC_Control.cs which inherits the base class name as Composite
public class MC_Control :
TextBox txtbox = new TextBox();
lbl = new Label();
Button btn = new Button();
string textCode Description:
txtbox.Text = value;
protected override void CreateChildControls()
lbl.Text = "User Name:<br>";
btn.Text = "Submit";
btn.BackColor = Color.Red;
In this code given above we are using two things; first a property and second a
method, Property which named is text which will set the text property of the
textbox whatever we write in the text property of the textbox it will display
value inside the textbox. Further we have to make a method named as
CreateChildControls (which is a method of base class Composite Control), will
override to implement some other changes into the controls we are making. It is
used to add the instances of the Controls which we have made above to the
container. In this method we can implement the text and color properties
of the controls that will apply to your composite control. You can add also more
properties which is you want to implement.
Step 5: Now after doing all
that we have to open a file named AssemblyInfo.cs and write the code given
Step 6: Now we have to
build the application by click on build solution and close it.
Step 7: Now we have to
take a ASP.NET Empty web application to test the application.
Step 8: Now we will add
a new project
Go to Solution Explorer
Right Click on it and add existing project
Add the Composite_control project to the website project.
Now it will appear like that.
Step 9: Right click on
Solution Explorer and set as start up project.
Step 10: Now Right
Click on Website project and add reference of the project name as
Step 11: Now it will
appear into the Composite_control Component which is at the top of the toolbox.
Step 12: Now if you
want to add it into the Toolbox Control then
Right Click on any control of Toolbox.
And Select Choose item.
Select the .NET Component and browse the button add the Composite_control.dll to the .NET Component
Now the component will be added to the .net framework component.
Click OK let see how it will appear
Step 13: Further you
have to drag and drop the derived control from the toolbox and you see that in
the source file of webform1.aspx there is something added like as a directive
and a instance of control information shown in the source file of webform1.aspx,
it will seem like as given below. You can add this register directive to the
other pages to access the control.
<%@ Register assembly="Composite_control" namespace="Composite_control" tagprefix="CC" %>
<CC:MC_Control ID="MC_Control1" runat="server" style="z-index:
1; left: 10px;
absolute" BackColor="#FF9966" BorderColor="#006666" BorderStyle="Groove" />
Step 14: Now you will
run the application by pressing F5.