Using AjaxUploader with a DetailsView (upload/insert at same time)

Last post 08-20-2009, 1:13 AM by cutechat. 1 replies.
Sort Posts: Previous Next
  •  08-14-2009, 10:33 AM 54697

    Using AjaxUploader with a DetailsView (upload/insert at same time)

    Hi!
     
    I need to use an AjaxUploader as part of a DetailsView. Users need to enter information and choose the file they want to upload (I have ManualStartUpload set to true) before trying to insert the new record.
     
    I have a couple of problems, though.
     
    1) I need to be able to validate that they have selected a file. I haven't been able to find any members or properties on the Uploader that would let me do this. The validation really needs to happen server side, so a JavaScript-only solution isn't a fix (although JavaScript for a client-side check would be fine).
     
    2) I'm not sure how to manually start the upload once they've entered all of the information. 
     
     I had 2 different ideas on how to set up my form:
     
    The first was to hook the OnClick event for the button I'm using as the upload (InsertButton) button on the uploader. Unfortunately, all OnClick events seem to be ignored, and I wouldn't know how to stop the Browse file dialog from starting while I validate (stopping the browse file dialog from opening if they hadn't filled in all info), then manually start that dialog later after validating.
     
    My second idea (which is probably a better idea than the first anyway) was to set ManualStartUpload to false and use the Insert button (or really, a generic button with a command works too) for the DetailsView. The problems I have with this way are: I need a way to validate that they have selected a file (with the proper extension) using the upload control, and then I need to be able to call a method to start the upload. I'll probably have to use the FileUploaded event to insert the rest of the information into the database since I'm not sure how the insert command would interact with the Uploader (I would suspect that the insert command wouldn't wait for the uploader to finish, thus causing a partially uploaded file).
     
     I really need for this information to be entered to the database and the file to be uploaded at the "same time" for the user, meaning all information should be correct on the form and they should have the file they want uploaded picked. If there's a better way of doing this, I'd be open to hearing that as well.
     
    Thanks!
  •  08-20-2009, 1:13 AM 54837 in reply to 54697

    Re: Using AjaxUploader with a DetailsView (upload/insert at same time)

    Hi,
     
    Please check this sample:
     
    1. <%@ Page Language="C#" %>  
    2.   
    3. <%@ Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>  
    4. <%@ Import Namespace="CuteWebUI" %>  
    5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    6.   
    7. <script runat="server">  
    8.     protected override void OnLoad(EventArgs e)   
    9.     {   
    10.         base.OnLoad(e);   
    11.   
    12.         if (!IsPostBack)   
    13.         {   
    14.             BindDetails();   
    15.         }   
    16.     }   
    17.   
    18.     private void BindDetails()   
    19.     {   
    20.         DetailsView1.DataSource = new DateTime[] { DateTime.Now };   
    21.         DetailsView1.DataBind();   
    22.     }   
    23.   
    24.     protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)   
    25.     {   
    26.         UploadPersistedFile pf=(UploadPersistedFile)DetailsView1.FindControl("uploader1");   
    27.         if (pf.File != null)   
    28.         {   
    29.             this.Title = "PageIndexChaging, saving " + pf.File.FileName;   
    30.         }   
    31.            
    32.         DetailsView1.PageIndex = e.NewPageIndex;   
    33.         BindDetails();   
    34.     }   
    35.   
    36.     protected void uploader1_FileValidating(object sender, UploaderEventArgs args)   
    37.     {   
    38.         if (args.FileSize>1000000)   
    39.             throw (new Exception(args.FileName+" is to big"));   
    40.     }   
    41.   
    42.     protected void btnSubmit_Click(object sender, EventArgs e)   
    43.     {   
    44.         UploadPersistedFile pf = (UploadPersistedFile)DetailsView1.FindControl("uploader1");   
    45.         if (pf.File != null)   
    46.         {   
    47.             //pf.File.CopyTo("NewFolder/"+pf.File.FileName);//MoveTo   
    48.             labelMsg.Text = "File saved : " + pf.File.FileName;   
    49.         }   
    50.         else   
    51.         {   
    52.             labelMsg.Text = "No file!";   
    53.         }   
    54.     }   
    55. </script>  
    56.   
    57. <html xmlns="http://www.w3.org/1999/xhtml">  
    58. <head runat="server">  
    59.     <title>Untitled Page</title>  
    60.     <script type="text/javascript">  
    61.     var uploader;   
    62.     function CuteWebUI_AjaxUploader_OnInitialize()   
    63.     {   
    64.         uploader=this;   
    65.     }   
    66.     function CuteWebUI_AjaxUploader_OnPostback()   
    67.     {   
    68.         document.getElementById('<%=btnSubmit.ClientID %>').click();   
    69.         return false;   
    70.     }   
    71.     function StartUpload()   
    72.     {   
    73.         if(uploader.getqueuecount()>0)   
    74.             uploader.startupload();   
    75.         else   
    76.             alert("please select a file");   
    77.     }   
    78.     </script>  
    79. </head>  
    80. <body>  
    81.     <form id="form1" runat="server">  
    82.         <div>  
    83.             <asp:DetailsView runat="server" ID="DetailsView1" AllowPaging="True" OnPageIndexChanging="DetailsView1_PageIndexChanging">  
    84.                 <Fields>  
    85.                     <asp:TemplateField HeaderText="Attachment">  
    86.                         <ItemTemplate>  
    87.                             <CuteWebUI:UploadPersistedFile id="uploader1" ManualStartUpload="True" runat="server" ItemTextTemplate="{0} {1} " OnFileValidating="uploader1_FileValidating" />  
    88.                         </ItemTemplate>  
    89.                     </asp:TemplateField>  
    90.                 </Fields>  
    91.             </asp:DetailsView>  
    92.             <asp:Button runat=server id="btnSubmit" Text="Submit" Style="display:none;" OnClick="btnSubmit_Click" />  
    93.             <button onclick="StartUpload();return false;">save profile</button>  
    94.             <asp:Label runat=server ID="labelMsg" />  
    95.         </div>  
    96.     </form>  
    97. </body>  
    98. </html>  

    Regards,
    Terry
     
View as RSS news feed in XML