File-Upload Custom Control in ASP. NET


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.

server control application

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;

        }
    }
}

Code Description: 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 File Upload composite control. 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.

Step 5: Now after doing all that we have to open a fine named as AssemblyInfo.cs and write the code given below.

  • Firstly add a namespace to the Top of the AssemblyInfo.cs file named as

        using System.Web.UI;

  • Now write the assembly name at the end of the AssemblyInfo.cs file.

       [assembly: TagPrefix("ServerControl1", "FUPLD")].

Step 6: Now we have to build the application by click on build solution and close it.

Step 7: Now we have to create an ASP.NET Empty web application to test the control.

  • File->New->ASP.NET web site.

  • Click OK.

website application

Step 8: Now we will add a new project

  • Go to Solution Explorer

  • Right Click on it and add existing project

  • Add the ServerControl1 project to the website project.

add project

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 ServerControl1 .

  • Go to add reference.

  • And select Project then add project named as ServerControl1.

project

Step 11: Now it will appear into the  file upload Composite_control Component which is at the top of the toolbox.

upload component

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 ServerControl1.dll to the .NET Component

  • Now the component will be added to the .NET framework component.

assembly reference
  • Now the control will appear in the toolbox.

Upload control in toolbox

Step 13: Further you have to drag and drop the file upload composite control from the toolbox and you see that in the source file of default.aspx there is something added like as a directive and a instance of control information shown in the source file of default.aspx, it will seem like as given below. You can add this register directive to the other pages to access the control.

<%@ Register assembly="ServerControl1" namespace="ServerControl1" tagprefix="FUPLD" %>

<CC:AdFup ID="AdFup1" runat="server" style="z-index: 1; left: 10px; top: 15px; position: absolute" BackColor="#FF9966" BorderColor="#006666" BorderStyle="Groove" />

Step 14: Now you will run the application by pressing F5.

Output:

Output

Up Next
    Ebook Download
    View all
    Learn
    View all