文本框没有更新
本文关键字:更新 文本 | 更新日期: 2023-09-27 18:11:04
我在aspx页面的更新面板中有一个文本框
下面是我的代码:场景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;
}
}
第二幕:
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;
}
}
在这两种情况下,我都没有看到开始上传文本,但我看到文件上传成功的文本。
如何让用户知道上传开始了,因为可能需要一段时间。我不能使用进度条,因为我使用YouTube API,它使用同步。上传。我不需要进度条,我只想查看一个文本来知道上传已经开始的用户。怎么做呢?谢谢你。
如果您只是想让USER 知道文件正在上传:这里有一种方法可以做到这一点,而不需要编写太多特殊的客户端或服务器代码本身。
因为你已经有一个UpdatePanel,你现在只需要添加一个UpdateProgress
与一个漂亮的图像和标题:
ASPX:
<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>
您也可以查看 this
和 this
。
它不会立即更新面板。整个方法执行,然后您将看到成功上传的消息,因为这是您最后设置文本的内容。
要查看实际操作,请尝试删除这一行:
YoutubeStatusLabel.Text = "Files were uploaded successfully.";
但是,它会在完成后显示"开始上传"。这可不好!所以使用客户端脚本设置"开始上传…"文本
看不到文本的原因是您从未向用户发送信息。
基本上,这个过程是这样的:
- 您的请求到达服务器
- 服务器处理请求并创建所有HTML文本(此时该方法已经完全运行,因此
YoutubeStatusLabel.Text
将是它在UploadPromoFilesButton_Click
方法中设置的最后一件事) - HTML文本被发送回客户端
如果想要显示中间文本,则需要在客户端(JavaScript)设置该值,或者向服务器多次发回。最后一个选项也需要JavaScript。
我会研究SignalR以获得更高级的客户端服务器通信。
您不会看到YoutubeStatusLabel.Text
的第一次更新,因为您的代码正在服务器上执行,并且尚未返回到浏览器。
页面生命周期值得一读。
我看不出通知用户上传已经开始,然后在5秒后再通知用户有什么好处。我会将文本设置为Starting upload...
,然后在过程完成后进行回调,将标签更新为Files were uploaded successfully.
。看到