如何向在 class ASP.NET C# 中创建的控件添加属性?

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

我的 App_Code 文件夹中有这个。我正在创建一个用于上传文件的自定义控件。我需要将 AllowMultiple 属性 添加到 FileUpload 控件。我该怎么做?看代码里的注释看fileupload在哪里,从msdn网站上搞不懂怎么办。

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();
            }
        }

如果您使用的是 .NET Framework 4.5+,那么 属性 已经存在:

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

请注意,我认为这是 HTML5 相关的。

如果没有,您可以使用 Attributes 属性 添加将在页面上呈现的其他 HTML 属性。

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

如果您的网站使用 HTML5,这将 有效。

您还可以从 FileUpload 派生一个新的 class,将 属性 添加到那个 class,然后覆盖 AddAttributesToRender(或其他呈现方法之一)方法以输出合适的HTML.