New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Removing Selected File in RadAsyncUpload for ASP.NET AJAX

Environment

PropertyValue
ProductRadAsyncUpload for ASP.NET AJAX
Versionall

Description

When using a RadAsyncUpload control in a RadWindow, you may encounter a scenario where a selected file is still retained in the control when the window is closed and reopened. This article provides a solution to remove the selected file server-side when the window is closed.

Solution

To remove the selected file from the RadAsyncUpload control when the RadWindow is closed, you can use the deleteAllFileInputs() method provided by RadAsyncUpload. Follow the steps below to implement this solution:

  1. Add the following JavaScript function to your page:
javascript
function onCloseHandler(sender, args) {
    var asyncUpload = $find('<%= RadAsyncUpload1.ClientID %>');

    // Clear all selected files
    asyncUpload.deleteAllFileInputs();
}
  1. In the markup of your RadWindow, make sure to specify the OnClientClose attribute to call the JavaScript function when the window is closed:
html
<telerik:RadWindow ID="RadWindow1" runat="server" OnClientClose="onCloseHandler">
    <!-- Window content here -->
</telerik:RadWindow>
  1. To register the JavaScript code block on the server side, you can use the RegisterStartupScript method. Here's an example of how you can do it in the Page_Load event:
csharp
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RegisterStartupScript();
    }
}

private void RegisterStartupScript()
{
    string script = @"
        <script type='text/javascript'>
            function onCloseHandler(sender, args) {
                var asyncUpload = $find('" + RadAsyncUpload1.ClientID + @"');
                
                // Clear all selected files
                asyncUpload.deleteAllFileInputs();
            }
        </script>";

    ScriptManager.RegisterStartupScript(this, GetType(), "onCloseScript", script, true);
}

With this solution in place, the selected file will be removed from the RadAsyncUpload control when the RadWindow is closed, ensuring that no files are retained when the window is reopened.

See Also