|
| 1 | +--- |
| 2 | +title: Clear Uploaded Files From The Server |
| 3 | +description: This article demonstrates how to reset the RadAsyncUpload control to its initial state with no files selected using server-side code. |
| 4 | +type: how-to |
| 5 | +page_title: Clear Uploaded Files From The Server |
| 6 | +slug: asyncupload-clear-uploaded-files-from-the-server-side |
| 7 | +tags: radasyncupload, asp.net ajax, server-side, clear, reset |
| 8 | +res_type: kb |
| 9 | +ticketid: 1668634 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadAsyncUpload for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +I have a [RadAsyncUpload](https://docs.telerik.com/devtools/aspnet-ajax/controls/asyncupload/overview) control on an ASP.NET Web form along with other controls. I need to clear out all the controls after some server-side code is called after a button click. For other controls, I can easily set their value to an empty string or their default state. I would like to reset the RadAsyncUpload control to its initial state, with no files selected. |
| 30 | + |
| 31 | +This KB article also answers the following questions: |
| 32 | + |
| 33 | +- How can I reset RadAsyncUpload after submitting the form? |
| 34 | +- Is it possible to clear the RadAsyncUpload control using server-side code? |
| 35 | +- How to remove all files from RadAsyncUpload from the server-side? |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +To clear the RadAsyncUpload control and reset it to its initial state using server-side code, you can call a JavaScript function that removes the uploaded files from the client-side. This approach involves using a RadButton for postback and a RadAjaxManager to update the controls without a full page postback. |
| 40 | + |
| 41 | +First, define the RadAsyncUpload, RadButton, and RadAjaxManager in your ASPX page as follows: |
| 42 | + |
| 43 | +````ASP.NET |
| 44 | +<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> |
| 45 | + <AjaxSettings> |
| 46 | + <telerik:AjaxSetting AjaxControlID="RadButton1"> |
| 47 | + <UpdatedControls> |
| 48 | + <telerik:AjaxUpdatedControl ControlID="RadButton1" /> |
| 49 | + </UpdatedControls> |
| 50 | + </telerik:AjaxSetting> |
| 51 | + </AjaxSettings> |
| 52 | +</telerik:RadAjaxManager> |
| 53 | +
|
| 54 | +<telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server"></telerik:RadAsyncUpload> |
| 55 | +<telerik:RadButton runat="server" ID="RadButton1" Text="Postback" AutoPostBack="true" OnClick="RadButton1_Click" /> |
| 56 | +```` |
| 57 | + |
| 58 | +Next, add the following JavaScript function to your page. This function uses the client-side API of RadAsyncUpload to clear the files: |
| 59 | + |
| 60 | +````JavaScript |
| 61 | +function clearUploadedFiles() { |
| 62 | + let upload = $find("<%= RadAsyncUpload1.ClientID %>"); |
| 63 | + upload.deleteAllFileInputs(); |
| 64 | +} |
| 65 | +```` |
| 66 | + |
| 67 | +Finally, call this JavaScript function from the server-side event handler of the button click event. Use the `ScriptManager.RegisterStartupScript` method to execute the JavaScript function from the server-side code: |
| 68 | + |
| 69 | +````C# |
| 70 | +protected void RadButton1_Click(object sender, EventArgs e) |
| 71 | +{ |
| 72 | + ScriptManager.RegisterStartupScript(this, GetType(), "key", "clearUploadedFiles();", true); |
| 73 | +} |
| 74 | +```` |
| 75 | + |
| 76 | +By following these steps, you can successfully reset the RadAsyncUpload control to its initial state with no files selected after a server-side event. |
| 77 | + |
| 78 | +## See Also |
| 79 | + |
| 80 | +- [RadAsyncUpload Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/asyncupload/overview) |
| 81 | +- [RadAsyncUpload Client-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/asyncupload/client-side-programming/overview) |
| 82 | +- [RadAjaxManager Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/ajaxmanager/overview) |
0 commit comments