Sitefinity CMS

Creating User Controls Send comments on this topic.
See Also
Developing with Sitefinity > Controls > Adding New Controls to Sitefinity > User Controls > Creating User Controls

Glossary Item Box

User Controls are classes which inherit from System.Web.UI.UserControl. To add a custom code or controls to pages in a Sitefinity site, developers could create and upload ASP.NET User Controls (.ascx files). Below are instructions on how to create a simple User Control that wraps a RadUpload control for file uploading.

 

Creating a User Control in Visual Studio

In order to create a user control, the respective file should be created in and/or added to the Sitefinity project in Visual Studio:

  1. Launch Microsoft Visual Studio.
  2. Select File, then New, and then Web Site.
  3. In the New Web Site dialog box, select a location in the file system. Select the C# language.
  4. Click OK.
  5. Select the Web User Control template.
  6. Click Open.
  7. Right-click the Web site in Solution Explorer and select Add New Item.
  8. In the Add New Item dialog box, select the Web User Control template.
  9. Check the Place Code in Separate File check box.
  10. Select the C# language.
  11. Name the new item UploadControl.ascx.
  12. Click Add.

 

Adding Controls and Code to the User Control

The following code should be put into the user control so that to add the RadUpload control which is a ready-to-use control that uploads files: 

  1. Select the Design tab of UploadControl.ascx.
  2. Drag a RadUpload control from the Toolbox to the UploadControl.ascx design surface.
  1. Select the Source tab of UploadControl.ascx.
  2. Modify the source to add a submit button after the RadUpload control, as follows:

     

    RadUpload

    Copy Code
    <radU:RadUpload ID="RadUpload1" runat="server"/><br/>
    <
    br/>
    <
    asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>   

     

  1. Right-click the design surface and select View Code.
  2. Add the following code to the UserControl, just above the existing Page_Load method:

     

    C# Copy Code
    public string TargetFolder{    get    {        return RadUpload1.TargetFolder;    }    set    {        RadUpload1.TargetFolder = value;    }}     
    protected void btnSubmit_Click(object sender, EventArgs e){    bool resultsExist = RadUpload1.UploadedFiles.Count > 0;}   
  1.  Select Build Solution from the Build menu.

 

For instructions on how to upload and use this user control, see Uploading User Control to Toolbox

See Also