Introduction:
Composite Custom Control are those control which contain another controls means there is like as controls inside a control. It is almost like as user custom control in ASP.NET which is used like as a container to keep another control in it. Here we are going to create the file upload composite control them later use it 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 given below.
Step 1: Firstly we are going to open the Web Server Control application
-
Go to Visual Studio and open File->New->Project->Add ASP.NET Server Control application.
-
Click OK.
-
Give the name as Composite_control.
Step 2: Now you have to change the name of ServerControl.cs file to AdFup.cs.
Step 3: Further
Toolbox Data attribute for the class, change the
string "ServerCOntrol1" to "AdFup.cs" in both places where it occurs.
[ToolboxData("<{0}:AdFup.cs runat=server></{0}:AdFup.cs>")]
Step 4: Now write the below code for the class named AdFup.cs which inherits the base class name as Composite Control, InamingContainer.
Code:
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Linq;
using
System.Text;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Configuration;
using System.IO;
namespace
ServerControl1
{
[DefaultProperty("Text"),
ToolboxData("<{0}:AdFup
runat=\"server\"> </{0}:AdFup>")]
public class
AdFup :
CompositeControl, INamingContainer
{
protected
Label lbl;
protected
FileUpload fu;
public AdFup()
{
}
[Bindable(true),
Category("Miscellaneous"),
DefaultValue(""),
Localizable(true)]
public
string LText
{
get { EnsureChildControls();
return lbl.Text; }
set { EnsureChildControls();
lbl.Text = value; }
}
[Bindable(true),
Category("Miscellaneous"),
DefaultValue(""),
Localizable(true)]
public string
FName
{
get
{
string s = (string)ViewState["FName"];
if (s ==
null)
{
return
string.Empty;
}
else
{
return s;
}
}
set { ViewState["FName"]
= value; }
}
[Bindable(true),
Category("Miscellaneous"),
DefaultValue(""),
Localizable(true)]
public string
FPath
{
get
{
string s = (string)ViewState["FPath"];
if (s ==
null)
{
return
string.Empty;
}
else
{
return s;
}
set { ViewState["FPath"]
= value; }
}
[Bindable(true),
Category("Miscellaneous"),
DefaultValue(""),
Localizable(true)]
public string
FCheck
{
get
{
string s = (string)ViewState["FCheck"];
if (s ==
null)
{
return
string.Empty;
}
else
{
return s;
}
}
set { ViewState["FCheck"]
= value; }
}
[Bindable(true),
Category("Miscellaneous"),
DefaultValue(""),
Localizable(true)]
public string
Ext
{
get
{
string s = (string)ViewState["Ext"];
if (s ==
null)
{
return
string.Empty;
}
else
{
return s;
}
}
set { ViewState["Ext"]
= value; }
}
public AdFup(string
labelText, string fileName,
string filePath,
string extension)
{
LText = labelText;
FName = fileName;
FPath = filePath;
Ext = extension;
}
protected
override void CreateChildControls()
{
Controls.Clear();
lbl = new
Label();
Controls.Add(lbl);
fu = new
FileUpload();
Controls.Add(fu);
fu.BackColor =
Color.Chocolate;
}
}
}


Join the conversation! Your thoughts help the community grow.