AjaxFileUpload control doesn't work if it's placed on an initially invisible Panel control. Files can be selected on the client side, but if the user clicks the Upload button then the upload will not be finished and it remains in a failed state.
The following example demonstrates how can the problem be reproduced:
<script type=”text/javascript”>
function onClientUploadComplete(sender, e) {
}
</script>
<asp:Button ID="bt1" runat="server" Text="Show upload panel" OnClick="bt1_Click" />
<asp:Panel ID="p1" runat="server" Visible="false">
<asp:AjaxFileUpload ID="ajaxFileUpload" runat="server"
OnClientUploadComplete="onClientUploadComplete"
OnUploadComplete="ajaxFileUpload_OnUploadComplete"
MaximumNumberOfFiles="1" />
</asp:Panel>
------------- Code behind --------------
protected void bt1_Click(object sender, EventArgs e)
{
p1.Visible = true;
}
protected void ajaxFileUpload_OnUploadComplete(object sender, AjaxFileUploadEventArgs file)
{
}
The following script error will be experienced if the user tries to upload any file onto the server:
Sys.ArgumentException: Sys.ArgumentException: Cannot deserialize. The data does not correspond to valid JSON. Parameter name: data
The problem can be worked around by placing another AjaxFileUpload control inside an invisible HTML div tag on the same page attaching the same client and server side upload complete event handlers to both AjaxFileUpload control.
<div style="display:none">
<asp:AjaxFileUpload ID="ghostAjaxFileUpload" runat="server"
OnClientUploadComplete="onClientUploadComplete"
OnUploadComplete="ajaxFileUpload_OnUploadComplete"
/>
</div>
AjaxControlToolkit:
Runtime Version: v4.0.30319
Version: 4.1.60919.0
Target framework: .NET Framework 4.0
Comments: Thank you! It got me on the right track.. I had the same script error, caused in my case by changing the _MaximumNumberOfFiles_ property during _IsInFileUploadPostBack_. My GridView is binding to a SQLDataSource, added a check for _IsInFileUploadPostBack_ before changing anything: ``` Protected Sub gvFiles_DataBound(sender As Object, e As System.EventArgs) _ Handles gvFiles.DataBound If afu.IsInFileUploadPostBack = False Then Dim atcCount As Int16 = gvFiles.Rows.Count If atcCount > 0 Then afu.MaximumNumberOfFiles = gvFiles.Rows.Count Else afu.Enabled = False End If End If End Sub ```
The following example demonstrates how can the problem be reproduced:
<script type=”text/javascript”>
function onClientUploadComplete(sender, e) {
}
</script>
<asp:Button ID="bt1" runat="server" Text="Show upload panel" OnClick="bt1_Click" />
<asp:Panel ID="p1" runat="server" Visible="false">
<asp:AjaxFileUpload ID="ajaxFileUpload" runat="server"
OnClientUploadComplete="onClientUploadComplete"
OnUploadComplete="ajaxFileUpload_OnUploadComplete"
MaximumNumberOfFiles="1" />
</asp:Panel>
------------- Code behind --------------
protected void bt1_Click(object sender, EventArgs e)
{
p1.Visible = true;
}
protected void ajaxFileUpload_OnUploadComplete(object sender, AjaxFileUploadEventArgs file)
{
}
The following script error will be experienced if the user tries to upload any file onto the server:
Sys.ArgumentException: Sys.ArgumentException: Cannot deserialize. The data does not correspond to valid JSON. Parameter name: data
The problem can be worked around by placing another AjaxFileUpload control inside an invisible HTML div tag on the same page attaching the same client and server side upload complete event handlers to both AjaxFileUpload control.
<div style="display:none">
<asp:AjaxFileUpload ID="ghostAjaxFileUpload" runat="server"
OnClientUploadComplete="onClientUploadComplete"
OnUploadComplete="ajaxFileUpload_OnUploadComplete"
/>
</div>
AjaxControlToolkit:
Runtime Version: v4.0.30319
Version: 4.1.60919.0
Target framework: .NET Framework 4.0
Comments: Thank you! It got me on the right track.. I had the same script error, caused in my case by changing the _MaximumNumberOfFiles_ property during _IsInFileUploadPostBack_. My GridView is binding to a SQLDataSource, added a check for _IsInFileUploadPostBack_ before changing anything: ``` Protected Sub gvFiles_DataBound(sender As Object, e As System.EventArgs) _ Handles gvFiles.DataBound If afu.IsInFileUploadPostBack = False Then Dim atcCount As Int16 = gvFiles.Rows.Count If atcCount > 0 Then afu.MaximumNumberOfFiles = gvFiles.Rows.Count Else afu.Enabled = False End If End If End Sub ```