简体   繁体   中英

Ajax AsyncFileUpload fires server code but does not update client web page

I have an asyncfileupload control inside an update panel. The file succesfully upload and fires the correct server side code. The code on the server is exected as expected however, one line in the server code changes the text on a label. I step through the code in debug mode and the line is executed but no change is made to the page.

Here's some of the code:

<asp:UpdatePanel runat="server" ID="updater" > 
    <ContentTemplate> 
        <asp:AsyncFileUpload ID="fileUpload" runat="server" OnUploadedComplete="FileUploadComplete" /> 
        <asp:Label ID="AsyncText" runat="server" Text="File Type not checked" />  
    </ContentTemplate> 
</asp:UpdatePanel> 
public void FileUploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{ 
    System.Threading.Thread.Sleep(500); 
    if(fileUpload.HasFile) { AsyncText.Text = "file of correct format: "; } 
}

Can anyone help me with solving this problem or offering annother solution??

Thanks

Where is the label positioned, inside or outside the update panel? Seems like the partial page update may not be including the update to the label text. I would say move the label around as the simplest suggestion, but you could also try something like RegisterStartupScript which will change the lable text via javascript. This should still give you server side control over what text to display based on what happens during the upload.

If you could post some code that would be great.

I think you are going to have to move toward a different solution. From your label message, it looks like you are trying to check the file type, correct? Basically, the AsyncUplaod control, although posting back to get the uploaded file to the browser, is not actually updating the page's viewstate, thus the label never gets updated. Boo! I was able to visualize this using this code in the page load event.

if (Page.IsPostBack)
{
    if (Request.Files.Count > 0)
    {
        AsyncText.Text = "file of correct format";
        ListItem item = new ListItem("item to add");
        lb.Items.Add(item);
    }
}

This was allowing me to set the label text but still nothing changed until I clicked on a random button that I added to the page. This button didnt even have an event in the code behind, but it was enough to cause a normal postback, and the label text and list item were successfully updated/added to the list. With that said, I would wait to update any labels until the user clicks upload by using a seperate upload button. (ie use the AsyFileUplaod to get it to the browser, but another button to save the file to the server). You can always do file evaluations in the button click event by referencing the posted files to the webpage as I did in the code above.

Some other examples I found online were using javascript to change the label text which works well also. Somthing like this:

string message = "";
if (e.StatusMessage == "Success")
{
   message = "File upload successful;";
}
else
{
   message = "File did not upload successfully;";
}
ClientScript.RegisterStartupScript(this.GetType(), "akey", "document.getElementByID('label').value =" + message, true);

Another example: here

I think in this case it's just the nature of the control and the only way to achieve what you want is though some creative thinking. If you have any other questions about anything I listed here feel free to ask.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM