2011-12-08 130 views
0

我已经创建了一个用户控件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 />"; 
      } 
     } 
    } 

回答

1

你可以把你的控制下这里的详细步骤一个dll: Turning an .ascx User Control into a Redistributable Custom Control

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

0

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

旧文章,但很清楚,例如,校长:

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

+0

怎么样.js文件? – Neo

+0

只需通过覆盖渲染将链接添加到它们 – deadcrab

1

您可以通过自定义控制项目

  • 右键单击该dll嵌入他们即

    1. 包括他们作为正常的,然后选择“嵌入的资源添加js文件'
    2. 通过资源管理器访问,因为它们现在是默认资源文件的一部分,例如

    Stream ms = Assembly.GetExecutingAssembly() .GetManifestResourceStream(“resourcename including namespace”);

    1. 然后读取流,以获得脚本作为字符串
    2. 注册脚本字符串的ScriptManager通常的方式
  • 相关问题