Take the 2-minute tour ×

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:

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

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.

share|improve this question
add comment

3 Answers

This is pretty easy with jQuery. Have an html hidden input control placed in your page, not the asp:hidden input control. Add a class lets say "hiddenPhoto" to your html hidden control.

so lets say our control html is like this

<input type="hidden" class="hiddenPhoto" runat="server" id="hdPhotoName" />

Now access it using class selector in your OnClientUploadComplete js method and set its value. Have it declared runat="server" in order to access its value on the server side.


share|improve this answer
add comment
up vote 0 down vote accepted

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:*

         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)

        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 = 
                    // Set the ImageID1 in the session
                Session["ImageID1"] = pimg.IdImageGroup.ToString();
                //  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:

    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".

share|improve this answer
add comment

The better and more simple solution is in code behind:

string script = String.Format("top.document.getElementById('hdnFilename').value='{0}';", safeFilename);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "hdnFilenameFromCodeBehind", script, true);

In my case, safeFilename is the unique filename, after handling duplicate filename, i.e. sample_5.png in the 5th upload of sample.png.

See http://forums.asp.net/t/1503989.aspx

share|improve this answer
In a similar way, you could call a javascript function. –  Tillito Jan 22 at 10:43
add comment

Your Answer


