Reputation: 93
I have a textbox within a update panel in a aspx page
and here's my code:
scene 1:
protected void UploadPromoFilesButton_Click(object sender, EventArgs e)
{
try
{
YoutubeStatusLabel.Text = "Starting upload...";
//UpdatePanel.Update();
if (!string.IsNullOrEmpty(UrlTagTextBox.Text))
{
var Movie = Vitagamer.Movies.DB.DBUtilities.Get.ByUrlTag(UrlTagTextBox.Text);
//Movie.UploadPromo();
}
else
{
//Vitagamer.Movies.PromotionHelper.Youtube.Execute.UploadAllPromoVideos();
}
System.Threading.Thread.Sleep(5000);
YoutubeStatusLabel.Text = "Files were uploaded successfully.";
}
catch (Exception Ex)
{
YoutubeStatusLabel.Text = Ex.Message;
}
}
scene two:
protected void UploadPromoFilesButton_Click(object sender, EventArgs e)
{
try
{
YoutubeStatusLabel.Text = "Starting upload...";
UpdatePanel.Update();
if (!string.IsNullOrEmpty(UrlTagTextBox.Text))
{
var Movie = Vitagamer.Movies.DB.DBUtilities.Get.ByUrlTag(UrlTagTextBox.Text);
//Movie.UploadPromo();
}
else
{
//Vitagamer.Movies.PromotionHelper.Youtube.Execute.UploadAllPromoVideos();
}
System.Threading.Thread.Sleep(5000);
YoutubeStatusLabel.Text = "Files were uploaded successfully.";
}
catch (Exception Ex)
{
YoutubeStatusLabel.Text = Ex.Message;
}
}
in both cases, i never seee that starting upload text but i see the files were uploaded successfully text.
how to let the user know that the uploading is starting since it may take a while. i can't use a progress bar since i'm using youtube api and it uses sync. upload. i don't need the progress bar, i just wanna view a text to know the user that upload has started. how to do it? thank u.
Upvotes: 0
Views: 179
Reputation: 4542
If you are just interested in letting the USER know a file is being uploaded: Here is a way to do it without needing to write too much special Client OR Server code per-se.
Since you already have an UpdatePanel, you now just need to add an UpdateProgress
with a nice image and caption:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UploadPromoFilesButton"
EventName="Click" />
</Triggers>
<ContentTemplate>
<!-- YOUR FILE UPLOAD ASPX GOES HERE -->
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<center>
<img src="../Content/img/CircularProgressAnimation.gif" />
<br /><i>Uploading file, please wait....</i>
</center>
</ProgressTemplate>
</asp:UpdateProgress>
You can also look at this
and this
as well.
Upvotes: 3
Reputation: 578
You won't see the first update to YoutubeStatusLabel.Text
as your code is executing on the server and has not been returned to the browser yet.
It would be worth reading up on the Page Lifecycle.
I don't see the benefit of notifying the user that the upload has started and then again after 5 seconds. I would set the text to Starting upload...
and then had a callback which updated the label to Files were uploaded successfully.
once the process is complete. See here
Upvotes: 1
Reputation: 7259
The reason you aren't seeing the text is because you never send information back to the user.
Essentially, the process is like this:
YoutubeStatusLabel.Text
will be the last thing it was set to in your UploadPromoFilesButton_Click
method)If you want to show the intermediate text, you need to either set the value on the client side (JavaScript) or do multiple post backs to the server. The last option would also require JavaScript.
I would look into SignalR for more advanced Client Server communication.
Upvotes: 1
Reputation: 32693
It doesn't instantly update the panel. The entire method executes, then you see the successfully uploaded message because that was the last thing you set the text to.
To see this in action, try removing this line:
YoutubeStatusLabel.Text = "Files were uploaded successfully.";
However, then it'll say "starting upload" AFTER it's done. That's no good! So use client side script to set the "Starting upload..." text.
Upvotes: 1