ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Clear contents of AsyncFileUpload Control after upload and page revisit
Author Name: Mudassar Khan Published Date: October 12, 2009
Filed Under :
ASP.Net
 |
AJAX
Views: 7102

Here I am explaining an issue that I just noticed on the http://forums.asp.net. There the person asked that he was not able to clear the values of the new ASP.Net AJAX Control Toolkit AsyncFileUpload control when the page is revisited, refreshed or reloaded. If you need to know more about using this control, please refer my article Using ASP.Net AJAX Control Toolkit’s AsyncFileUpload Control

 

After some research and looking closely how it works I found the trick to clear the contents of the AsyncFileUpload Control. Basically the control comprises of a readonly textbox. I did not find any property to clear the AsyncFileUpload control. Thus I took help of JavaScript programming.

The following JavaScript method clears the contents of the AsyncFileUpload Control

<script type = "text/javascript">

function clearContents() {

    var AsyncFileUpload = $get("<%=AsyncFileUpload1.ClientID%>");

    var txts = AsyncFileUpload.getElementsByTagName("input");

    for (var i = 0; i < txts.length; i++) {

        if (txts[i].type == "text") {

            txts[i].value = "";

            txts[i].style.backgroundColor = "white";

        }

    }

}

window.onload = clearContents;

</script> 

 

You can call this function wherever you want, above I am calling it on window onload event so that the control gets cleared each time page is reloaded, refreshed or revisited. In case you want to clear it after the file is uploaded on the server call it up on the OnClientUploadComplete event of the AsyncFileUpload control in the following way.

<script type = "text/javascript">

function uploadComplete(sender) {

    clearContents();

}

</script>

 

The GIF below describes how this trick clears the ASP.Net AJAX Control Toolkit AsyncFileUpload control when page is refreshed, reloaded, revisited and also once the file is uploaded


Clearing contents of ASP.Net AJAX Control Toolkit AsyncFileUpload control

That’s it. With this the article comes to an end. Let me know whether the above trick does not work for you


 

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Wojciech said:
Im beginers in ASP.Net programming so Im sorry for my maybe stupid question. Could you explain me where I have to put your code In aspx In C# or maybe instantly to OnClientUploadComplete event in control properties I have no idea and your solution seems to be great. br Regards. W.
February 03, 2010  

Mudassar Khan said:
Reply To: Wojciech
Hi,
The code is for ASPX Page within the Head Section And if you are using master pages place it anywhere in content place holder
February 03, 2010  

Subeesh said:
Thank you for your code.
May 22, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code