How to create custom control of existing user control

帅比萌擦擦* 提交于 2019-12-23 06:56:55

问题


I have created one user control for multiple file upload , i need to create its custom control so that I can have a dll of that control. What are the ways that I can do this?

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 />";
                }
            }
        }

I tried like following:

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:


You can put your control in a dll following the steps detailed here: Turning an .ascx User Control into a Redistributable Custom Control.

I think that it would be worth converting your user control to a proper server control, however, it's not that hard and you would end up with easier to maintain code (as you will see, the process described there is rather awkward).




回答2:


You could add the js files by embedding them in the dll i.e.

  1. Including them as normal in the custom control project
  2. Right clicking and selecting 'embedded resources'
  3. Accessing through the resource manager as they are now part of the default resource file i.e.

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

  1. Then read the stream to get the script as a string
  2. Register the script string with ScriptManager in the usual way



回答3:


To build a web control you need to inherit from UserControl or another control, in your case FileUpload, then override the init event to add other controls (e.g. button) to the tree. Override any other events as needed.

Old article but pretty clear e.g. of principal:

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



来源:https://stackoverflow.com/questions/8428462/how-to-create-custom-control-of-existing-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!