Search code examples
ajaxcontroltoolkitasyncfileupload

ajaxcontroltoolkit setting hidden value after asyncfileupload has completed


I have an asyncfileupload control that I'm using from the ajaxcontroltoolkit. On the file complete in the code behind I process the file and write the information in the file to a database. I get the id of the record from the database, and this needs to be written to an asp hidden field. I've tried just setting the value:

fldImageID.Value = pimg.IdImageGroup.ToString();

I've tried Registering a script like I've seen in an example on a website:

  ScriptManager.RegisterClientScriptBlock(
         ImageFileUploader, 
         ImageFileUploader.GetType(), 
         "script1",
       "alert('hi'); top.document.getElementById('" 
       + fldImageID.ClientID 
         + "').value='" 
       + pimg.IdImageGroup.ToString() 
       + "'; top.document.getElementById('" 
     + lblError.ClientID 
        + "').innerHTML = 'image uploaded'",
   true);

I've just tried embedding javascript in a response.Write call from the method I've set to process the uploaded file. Nothing I've done has worked so far. After I've done everything the hidden field still does not contain the required value.


Solution

  • I found an acceptable solution back when I was working on this. And since then I've received emails from people who have had the same problem and have been asking if I found a solution. So I'm presenting it here, stripping out any extraineous code:

    From the user control that has the FileUpload control I first set the session variable on the back side in the FileUploadComplete handler:

    *in the ascx file (upload_chart.ascx) I have the AsyncFileUpload, what is important is the OnUploadComplete and the OnClientUploadComplete:*

    <ajaxToolkit:AsyncFileUpload 
             OnUploadedComplete="FileUploadComplete1" 
             OnClientUploadComplete="UploadComplete1" 
             ID="ImageFileUploader" 
             runat="server" />
    

    *in the code behind of the ascx file (upload_chart.ascx.cs) I handle the FileUploadComplete:*

    public void FileUploadComplete1(object sender, EventArgs e)
    {
    
        try
        {
            if (ImageFileUploader.FileBytes.Length > 0)
                {
    
                    // File data is in ImageFileUploaded.FileBytes
                        // Save it however you need to
                    // I saved it to a database, in a DBImage Object class I created
                        // DBImage is specific to my application
                        ODS.Entity.DBImage pimg = 
                            ODS.Data.DataRepository.SaveImageBytes(ImageFileUploaded.FileBytes);
                        // Set the ImageID1 in the session
                    Session["ImageID1"] = pimg.IdImageGroup.ToString();
                }
                else
                {
                    //  error handling for an empty file, however you want to handle it
    
                }
        }
        catch (Exception Ex)
        {
            //  error handling for an unhandled exception, whatever you want to do here
    
        }
    }
    

    Javascript and script methods are used to set the value on the page, here is my codebehind for the script method:

     // on the aspx page code behind (chartofthedayadmin.aspx.cs) I have the webmethod:
    
       [System.Web.Services.WebMethod]
        public static string GetImageID1()
        {
            System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
            String retval = Session["ImageID1"].ToString();
            Session["ImageID1"] = null;
            return retval;
        }
    

    Here is the javascript: // on the aspx front end (chartofthedayadmin.aspx) I have the javascript // to call the Web method and the javascript failed message:

    function UploadComplete1() {
             var str = PageMethods.GetImageID1(uploadSuccess1, uploadFailed);
         }
        function uploadFailed() {
    
            alert('error occurred or some meaningfull error stuff');
       }
    

    *// javascript on the user control (upload_chart.ascx) to set the value of the hidden field*

     function uploadSuccess1(result) {
    
        document.getElementById('<%= fldImageID.ClientID %>').value = result;
    
    }
    

    note: Make sure your scriptmanager has EnablePageMethods="true".