如何创建现有用户控件的自定义控件

时间:2011-12-08 08:58:23

标签: c# asp.net custom-controls

我为multiple file upload创建了一个用户控件, 我需要创建自定义控件,以便我可以拥有该控件的dll。 我有什么方法可以做到这一点?

usercontrol.ascx

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
  <script src="Scripts/jquery.MultiFile.pack.js" type="text/javascript"></script>
    <div><%-- accept attribute can be used like accept="png|jpg"--%>
                    Multiple File Upload<br />
                    <asp:FileUpload ID="FileUpload10" runat="server" class="multi" accept="" />
                    <asp:Button ID="Button3" runat="server" Text="Submit" OnClick="jQueryUploadFiles" />

        <br />
        <asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="Green" />
        <br />
        <asp:Label ID="lblError" runat="server" EnableViewState="false" ForeColor="Red" />
    </div>

usercontrol.ascx.cs

  private void FileUploadUsingJQuerySelectionMethod()
        {
            // check if file has been selected
            HttpFileCollection files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];
                if (file.ContentLength > 0)
                {
                    string path = ConfigurationManager.AppSettings["FilePath"];
                    string fileName = Path.GetFileName(file.FileName);

                    // now save the file to the disk
                    file.SaveAs(path + fileName);

                    lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
                }
            }
        }

我尝试了以下内容:

public class MultipleFileUpload : WebControl
{
   #region declare controls here
    Label lblMessage;
    Label lblError;
    FileUpload FileUpload10;
    Button btnUpload;
   #endregion

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public string FilePath
    {// prop to get filepath
        get
        {
            String s = (String)ViewState["FilePath"];
            return ((s == null) ? "[" + this.ID + "]" : s);
        }

        set
        {
            ViewState["FilePath"] = value;
        }
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        output.Write(FilePath);
    }

   // create the layout (html) of your control here
   // all the HTML code including <div>
   // Add all controls to the <div>, below code is very crude.<br/>       
   // Also you need to register the script  tags and add the script to it<br/>

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        Table table = new Table();
        this.Controls.Add(table);
        lblMessage = new Label();
        lblMessage.ID = "lblMessage";

        lblError = new Label();
        lblError.ID = "lblError";

        FileUpload10 = new FileUpload();
        FileUpload10.ID = "FileUpload10";

        btnUpload = new Button();
        btnUpload.ID = "btnUpload";
        btnUpload.Text = "Submit <br/> ";
       // table.Controls.Add(lblMessage);
    }
    // invoke this method were ever required
    private void FileUploadUsingJQuerySelectionMethod()
    {
        // check if file has been selected
        HttpFileCollection files = HttpContext.Current.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (file.ContentLength > 0)
            {
                string path = FilePath;
                string fileName = Path.GetFileName(file.FileName);

                // now save the file to the disk
                file.SaveAs(path + fileName);

                lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以按照此处详述的步骤将控件放入dll:Turning an .ascx User Control into a Redistributable Custom Control

我认为将您的用户控件转换为适当的服务器控件是值得的,但是,它并不难,您最终会更容易维护代码(正如您将看到的,这里描述的过程相当尴尬)

答案 1 :(得分:1)

您可以通过将它们嵌入到dll中来添加js文件,例如

  1. 在自定义控制项目中正常包含它们
  2. 右键单击并选择“嵌入资源”
  3. 通过资源管理器访问,因为它们现在是默认资源文件的一部分,即
  4. Stream ms = Assembly.GetExecutingAssembly()                     .GetManifestResourceStream(“resourcename includes namespace”);

    1. 然后读取流以将脚本作为字符串
    2. 以通常的方式使用ScriptManager注册脚本字符串

答案 2 :(得分:0)

要构建Web控件,您需要继承UserControl或其他控件(在您的情况下为FileUpload),然后覆盖init事件以将其他控件(例如按钮)添加到树中。根据需要覆盖任何其他事件。

旧文章,但很明显,例如校长:

http://www.codeproject.com/KB/validation/textboxwithvalidator.aspx