Dynamic Uploaders / Programatically adding Uploaders

Last post 01-14-2010, 8:00 PM by cutechat. 3 replies.
Sort Posts: Previous Next
  •  12-05-2008, 7:01 PM 46629

    Dynamic Uploaders / Programatically adding Uploaders

    Are there any tricks or do you have a working example where you programatically add an Uploader to a page?

    The uploader works create when I add one to the aspx page but if I try to create one dynamically in code behind, the progress bar doesn't work correctly, the upload status never moves from 0kb, and the FileUploaded event never fires. The file does get uploaded however.  I make sure that on PostBack, the control is added back to the form and the FileUploaded event is hooked up. 

    Thanks,
    Phil 
     
    Example Code:
     
    CuteWebUI.Uploader uploader = new CuteWebUI.Uploader();
    uploader.InsertText = "Upload new Image";
    uploader.FileUploaded += new CuteWebUI.UploaderEventHandler(uploader_FileUploaded);
    uploader.ValidateOption.AllowedFileExtensions = "jpg,jpeg,gif,png";
    uploader.ID = ci.UniqueId;

     panel.Controls.Add(uploader); 
  •  12-06-2008, 7:54 AM 46631 in reply to 46629

    Re: Dynamic Uploaders / Programatically adding Uploaders

    Phil,
     
    Please check this sample code :
     
    <%@ Page Language="C#" %>

    <%@ Import Namespace="CuteWebUI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

     protected int UploaderCount
     {
      get
      {
       object val = ViewState["UploaderCount"];
       return val == null ? 0 : (int)val;
      }
      set
      {
       ViewState["UploaderCount"] = value;
      }
     }

     protected override void OnLoad(EventArgs e)
     {
      base.OnLoad(e);

      int c = UploaderCount;
      for (int i = 0; i < c; i++)
      {
       AddUploader(i);
      }
     }

     private void AddUploader(int index)
     {
      CuteWebUI.Uploader uploader = new CuteWebUI.Uploader();
      uploader.ID = "Uploader" + (index + 1);
      uploader.InsertText = uploader.ID;
      uploader.FileUploaded += new UploaderEventHandler(uploader_FileUploaded);
      Panel1.Controls.Add(uploader);
     }

     void uploader_FileUploaded(object sender, UploaderEventArgs args)
     {
      CuteWebUI.Uploader uploader = (CuteWebUI.Uploader)sender;
      InsertMsg(uploader.ID + " have upload file : " + args.FileName);
     }

     protected void ButtonAdd_Click(object sender, EventArgs e)
     {
      int c = UploaderCount;
      AddUploader(c);
      UploaderCount = c + 1;
     }

     void InsertMsg(string msg)
     {
      ListBoxEvents.Items.Insert(0, msg);
      ListBoxEvents.SelectedIndex = 0;
     }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
     <title>Untitled Page</title>
    </head>
    <body>
     <form id="form1" runat="server">
      <div>
       <asp:Button runat="Server" ID="ButtonAdd" Text="Add new uploader" OnClick="ButtonAdd_Click" />
      </div>
      <asp:Panel ID="Panel1" runat="server">
      </asp:Panel>
      <div>
       Server Trace:
       <br />
       <asp:ListBox runat="server" ID="ListBoxEvents" Width="400"></asp:ListBox>
      </div>
     </form>
    </body>
    </html>

     
     
    Regards,
    Terry
  •  01-14-2010, 12:10 PM 58171 in reply to 46631

    Re: Dynamic Uploaders / Programatically adding Uploaders

    Hi,
     
    I'm creating many uploaders dynamically in code behind to upload multiple files.
     
    The reasons for doing this and not 1 uploader for multiple files, are:
     
    1) Using an ajax form with many update panels

    2) It's necessary to know which file belongs to wich uploader, as the file will be used for a different purpose. I cannot rely on any information from the file itself like name, extension or size to identify its use.
     
     The problem with FileUploaded event args (UploaderEventArgs) is that I cannot fetch the ID of the control.
     
    Any ideas?
    Regards,
    Francisco
     
  •  01-14-2010, 8:00 PM 58175 in reply to 58171

    Re: Dynamic Uploaders / Programatically adding Uploaders

    Francisco,
     
    void Uploader_FileUploaded(object sender, UploaderEventArgs args)
     {
         Uploader uploader = (Uploader)sender;
         //get the uploader.ID  here
    }
     
    Regards,
    Terry
     
View as RSS news feed in XML