【问题标题】:How to add properties to controls that are created in a class ASP.NET C#?如何向在 ASP.NET C# 类中创建的控件添加属性?
【发布时间】:2015-05-20 02:03:23
【问题描述】:

我的 App_Code 文件夹中有这个。我正在创建一个用于上传文件的自定义控件。我需要将 AllowMultiple 属性添加到 FileUpload 控件。我该怎么做?查看代码中的 cmets 以查看文件上传的位置,从 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();
            }
        }

【问题讨论】:

    标签: c# asp.net app-code


    【解决方案1】:

    如果您使用的是 .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 派生一个新类,将属性添加到该类,然后重写 AddAttributesToRender(或其他呈现方法之一)方法以输出适当的 HTML。

    【讨论】:

    • 我还是个初学者,上面的代码不是我写的。你能带我过去吗?请问?
    • 你不需要添加它。它已经存在。只需设置 browser.AllowMultiple = true。
    • 我没有足够的信息比这更详细。如果您想了解更多详细信息,请至少告诉我您使用的 .NET 版本。
    • 没关系,我使用的是 Asp.net 2.0.5 并且不支持该属性。有解决办法吗?
    • 我已经修改了我的答案。由于您似乎没有使用 .NET 4.5,因此我建议使用 Attributes 属性来使文件控件获取多个文件。但是,这样做您很可能会遇到其他并发症。您将如何访问已上传的文件?我强烈建议您升级到 .NET 的更新版本,除非您正在处理大型现有项目。
    猜你喜欢
    • 2013-07-25
    • 2010-12-08
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2014-09-29
    • 2014-01-22
    • 2013-01-21
    • 1970-01-01
    相关资源
    最近更新 更多