Search code examples
c#asp.netapp-code

How to add properties to controls that are created in a class ASP.NET C#?


I have this in my App_Code folder. I am creating a custom control for uploading files. I need to add AllowMultiple property to the FileUpload control .How do I do it? See the comments in the code to see where the fileupload, cannot figure out how to do it from msdn website.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;

/// <summary>
/// Summary description for MultiFileUpload
/// </summary>
using System.IO;
using System.Configuration;
using System.Drawing.Imaging;
namespace MyControls
{
    [ToolboxData("<{0}:MultiFileUpload runat=server></{0}:MultiFileUpload>")]
    public class MultiFileUpload : CompositeControl
    {
        public string tempFolderPath;
        private FileUpload browser;
        private ListBox fileList;
        private Button addToListButton;
        private Button delFromListButton;
        private Button uploadFiles;
        private string uploadPath;
        public string thumbsPath;
      //  [BrowsableAttribute(true)]
       // public virtual bool AllowMultiple { get; set AllowMultiple=true; }
        protected override void CreateChildControls()
        {
// need to set AllowMultiple=true on here  for the fileupload. If there is a way aspx page that would work too        


            browser = new FileUpload();
            fileList = new ListBox();
            addToListButton = new Button();
            delFromListButton = new Button();
            uploadFiles = new Button();




            browser.Width = new Unit(350);
            fileList.Width = new Unit(265);
            addToListButton.Width = new Unit(75);
            delFromListButton.Width = new Unit(75);
            uploadFiles.Width = new Unit(353);





            addToListButton.Text = "Add";
            delFromListButton.Text = "Delete";
            uploadFiles.Text = "Upload to Site";

            addToListButton.Click += new EventHandler(AddToListButtonClick);
            delFromListButton.Click += new EventHandler(DelFromListButtonClick);
            uploadFiles.Click += new EventHandler(UploadFilesClick);

            this.Controls.Add(new LiteralControl("<table><tr><td colspan='2'>"));
            this.Controls.Add(browser);
            this.Controls.Add(new LiteralControl("<td></tr><tr><td rowspan='2' width='20'>"));
            this.Controls.Add(fileList);
            this.Controls.Add(new LiteralControl("</td><td>"));
            this.Controls.Add(addToListButton);
            this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));
            this.Controls.Add(delFromListButton);
            this.Controls.Add(new LiteralControl("</td></tr><table>"));
            this.Controls.Add(uploadFiles);

            base.CreateChildControls();
        }
        protected override void Render(HtmlTextWriter writer) {base.Render(writer);}
        public MultiFileUpload() {}
        public void SetUploadPath(string path) {this.uploadPath = path;}
        public string GetUploadPath() {return this.uploadPath;}

        private void AddToListButtonClick(object source, EventArgs e)
        {
            if (browser.HasFile) {
                DirectoryInfo tempFolder = new DirectoryInfo(tempFolderPath);
                if (tempFolder.Exists)
                {
                    browser.SaveAs(tempFolderPath + browser.FileName);
                }
            }
            RefreshListBox();
        }
        private void DelFromListButtonClick(object source, EventArgs e)
        {
            if (fileList.SelectedIndex != -1)
            {
                DirectoryInfo tempFolder = new DirectoryInfo(tempFolderPath);
                tempFolder.GetFiles().ElementAt(fileList.SelectedIndex).Delete();
                RefreshListBox();
            }
        }

Solution

  • If you are using .NET Framework 4.5+, then that property already exists:

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.allowmultiple(v=vs.110).aspx

    Note that I believe this is HTML5 dependent.

    If not, you can use the Attributes property to add additional HTML attributes that will be rendered on the page.

    browser.Attributes["multiple"] = "multiple"
    

    This will only work if you website is using HTML5.

    You could also derive a new class from FileUpload, add the property to that class, and then override the AddAttributesToRender (or one of the other render methods) method to output the appropriate HTML.