Ensuring OnFileUploaded Event Fires Automatically in RadAsyncUpload
Environment
| Product | AsyncUpload for UI for ASP.NET AJAX |
| Version | all |
Description
The server-side OnFileUploaded event of the RadAsyncUpload component fires only during a postback. If you want this event to trigger automatically once a file is uploaded — without requiring additional actions like a button click — you need to initiate the postback programmatically using the client-side OnClientFilesUploaded event.
Incorrect or incomplete AJAX settings, as well as certain configurations in Page_Load, can also prevent this event from firing as expected.
This knowledge base article also answers the following questions:
- How can I trigger the OnFileUploaded event without a button click?
- Why does the OnFileUploaded event not fire in RadAsyncUpload after a file upload?
- How do I configure RadAsyncUpload to refresh a grid after file upload?
Solution
Step 1: Correct Unsupported Configurations
Ensure that your RadAsyncUpload configuration is valid:
- Remove conflicts between
MultipleFileSelectionandMaxFileInputsCount:- Use
MultipleFileSelection="Automatic"and omitMaxFileInputsCount, or - Use
MaxFileInputsCountand setMultipleFileSelectionto"Single"or"Disabled".
- Use
Step 2: Trigger Postback from OnClientFilesUploaded
To trigger the postback programmatically after all files finish uploading:
- Add the
OnClientFilesUploadedevent to yourRadAsyncUploadcomponent. - Use the
__doPostBackmethod to initiate the postback from the client-side.
<telerik:RadAsyncUpload runat="server" ID="AsyncUpload1"
RenderMode="Lightweight"
MultipleFileSelection="Automatic"
ChunkSize="1048576"
HideFileInput="true"
MaxFileSize="104857600"
AllowedFileExtensions=".jpeg,.jpg,.png,.doc,.docx,.xls,.xlsx,.pdf,.xml,.eps,.pptx,.ppt"
EnableAjax="true"
TargetFolder="~/Files"
OnClientFilesUploaded="OnClientFilesUploaded"
OnFileUploaded="AsyncUpload1_FileUploaded" />
<script>
function OnClientFilesUploaded(sender, args) {
__doPostBack(sender.get_uniqueID(), "");
}
</script>
Step 3: Add AJAX Settings in Page_Load
Ensure that the AJAX settings are added on every request in your Page_Load handler. This ensures that the postback triggered via __doPostBack is handled correctly as a partial update.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Try
EnsureUploadFoldersExist()
Catch ex As Exception
PLog.Log(IDBType.IDBLog.ErrorLog, If(Session.SessionID, "NoSession"), _
"Upload folder creation error", ex.Message & " - " & ex.StackTrace)
End Try
' Register AsyncUpload1 with RadAjaxManager
Dim ajaxManager As RadAjaxManager = RadAjaxManager.GetCurrent(Page)
If ajaxManager IsNot Nothing Then
ajaxManager.AjaxSettings.AddAjaxSetting(AsyncUpload1, RGridAttachment)
ajaxManager.AjaxSettings.AddAjaxSetting(AsyncUpload1, RGridClipboard)
End If
End Sub
Step 4: Refresh the Grid in OnFileUploaded
In the server-side OnFileUploaded handler, rebind the grid to display the newly uploaded files.
Protected Sub AsyncUpload1_FileUploaded(sender As Object, e As FileUploadedEventArgs)
' RadAsyncUpload automatically saves e.File to TargetFolder.
' Just refresh the grid here to show the new file.
RGridAttachment.Rebind()
End Sub
Additional Notes
- Use
OnClientFilesUploaded(plural) instead ofOnClientFileUploadedto trigger a single postback after all files are uploaded. - Avoid manually saving the uploaded file inside
OnFileUploadedifTargetFolderis already set, as the file is automatically moved there. - Correct markup errors such as duplicate attributes or unsupported configurations.
See Also
- RadAsyncUpload Documentation
- RadAjaxManager Documentation
- RadAsyncUpload Client-Side Events
- RadAsyncUpload Server-Side Events