Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:41:07+00:00 2026-06-15T05:41:07+00:00

I have problems with my imageuplad using AjaxControlToolKit. I can upload files and display

  • 0

I have problems with my imageuplad using AjaxControlToolKit.
I can upload files and display them without postback but If the user changes his mind and uploads another image it still dispays the first image.Help would be appreciated.

This is my code:
Front:

<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>

    <script type="text/javascript">
        function uploadComplete(sender, args) {
            var pathForUploadedImage = $('#uploadedHiddenField').val();                               
            $get("uploadImageImg").src = $('#uploadedHiddenField').val();
            $(".uploadImage").fadeIn(100);

        }
</script>

<asp:Image id="uploadImageImg" class="uploadImage" alt="" runat="server" ClientIDMode="Static" />
<div id="uploadedImageDiv"></div>
<asp:Image ID="loader" runat="server" ImageUrl="~/library/images/loading.gif" Style="display: None" />
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="uploadComplete" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" CompleteBackColor="white" />
<asp:HiddenField ID="uploadedHiddenField" ClientIDMode="Static"  runat="server"/>

And Back:

 protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {

        try
        {

            Styling.SetupStyles();
            string virualFolder = "";
            if (string.IsNullOrEmpty(UploadDirectory))
            {
                virualFolder = "/" + Styling.GetStyleValue("customNewsImagesFolder", "") + "/";
            }
            else
            {
                virualFolder = "/" + Styling.GetStyleValue(UploadDirectory, "") + "/";
            }



            string fileExtension = Path.GetExtension(e.FileName);

            int fileSize = Convert.ToInt32(e.FileSize);

            string physicalFolder = Server.MapPath(virualFolder);

            string randomFileName = System.IO.Path.GetRandomFileName().Replace(".", "");

            bool isUnique = true;
            DirectoryInfo dir = new DirectoryInfo(MapPath(virualFolder));
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo myFile in files)
            {
                if (myFile.Name == randomFileName)
                {
                    isUnique = false;
                }
            }

            IsAllowedFileExtension(fileExtension);

            bool error = false;

            if (!isUnique)
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('En fil med detta namn finns redan, var vänlig och döp om din fil');</script>", false);
            }
            else if (!IsAllowedMaxFileSize(fileSize))
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('Filen är för stor, vänligen försök igen med en fil som är mindre än 2 MB');</script>", false);
            }
            else if (!IsAllowedFileExtension(fileExtension))
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('Felaktigt filformat, vänligen försök igen med en fil av följande typ: .jpg .jpeg .png .gif');</script>", false);
            }

            if (!error)
            {
                AsyncFileUpload1.SaveAs(physicalFolder + randomFileName + fileExtension);
                string thePath = (virualFolder + randomFileName + fileExtension).Replace("\\", "/");

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "filePath", "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);                

            }
        }
        catch (Exception ex)
        {
            erLog.LogException(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
    }

    private bool IsAllowedFileExtension(string fileExtension)
    {
        bool valid = false;
        if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg" || fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
            valid = true;
        return valid;
    }

    private bool IsAllowedMaxFileSize(int fileSize)
    {
        bool valid = false;
        if (fileSize < 2100000)
            valid = true;
        return valid;
    }

I think the problem has to do with that I’m displaying the image with the “ScriptManager.RegisterClientScriptBlock”-row but it doesnt seem to register the script if its already in the DOM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:41:08+00:00Added an answer on June 15, 2026 at 5:41 am

    You didn’t specify if your AsyncFileUpload1_UploadedComplete event handler is inside control or page. If it inside control than to fix your problem you can try to register client script using instance of the page. Other option is to generate unique script id every partial postback.

    Option 1 (register client script with page instance):

    ScriptManager.RegisterClientScriptBlock(this, this.Page, "filePath", "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);
    

    Option 2 (generate unique script id every partial postback):

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cancel button where the user can cancel on a file upload
I have problems with xcode and can't open xib files. Everytime I click on
We have problems playing audio files using JavaZoom's libraries on OpenJDK in Linux, although
I have problems with fragment when the screen orientation changes. In my code, I
I have problems with $pdo->execute($values). It works fine if I use $pdo->execute() without $values:
I have problems laying out the jCarousel pager (which I'm using along with Colorbox
I have problems using preventDefault. The problem is that this piece of code works
I have problems sending using a Dll procedure with parameters, im not allowed to
I have problems with moving files in directories in Java. The problem is that
I have problems with deleting a record from the screen using MSQLI. Here you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.