Search code examples
c#asp.netuser-controlscustom-server-controls

Accessing an ASP.Net user control


Hey guys,
I've created a CustomFileUpload class which is inherited from the original FileUpload class, I'm gonna have to say it's not actually a UserControl it's a simple class which can be seen below

using System;
using System.Web;

public class CustomFileUpload : System.Web.UI.WebControls.FileUpload
{
    public string Directory { get; set; }
}

I need to know how I can use the control in my page, maybe something like <@Registe ... when we create a usercontrol.


Solution

  • This is called a custom server control. You can read an indepth walkthrough about them. You will need to have the server control reside in an assembly that is separate from your project and then reference the assembly into your project.

    The syntax you want to use to put the control on the page is:

    <%@ Register Assembly="YourAssemblyName" TagPrefix="myControl" Namespace="YourNamespaceName"%>
    

    Then you can reference the control:

    <myControl:CustomFileUpload .... />
    

    An easy way to do this is to add the control to your toolbox. Then you can drag the control from the toolbox onto any page. The Register will be created automatically. To add the control to the toolbox

    1. Right click on the toolbox tab where the control should reside, select choose items. (You can create a new tab or use an existing one).
    2. Select Browse
    3. Browse to and select your assembly, click Open
    4. Your control should now appear in the toolbox tab.