TextBox不会更新

时间:2014-06-30 20:14:34

标签: c# asp.net textbox updatepanel

我在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并使用同步。上传。我不需要进度条,我只想查看文本以了解上传已启动的用户。怎么做?谢谢你。

4 个答案:

答案 0 :(得分:3)

如果您只是想让USER 知道正在上传文件:这是一种方法,无需编写太多特殊的客户端 OR 服务器代码本身

由于您已经拥有 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>  

您也可以查看thisthis

答案 1 :(得分:1)

它不会立即更新面板。整个方法执行,然后您看到成功上传的消息,因为这是您设置文本的最后一件事。

要查看此操作,请尝试删除此行:

YoutubeStatusLabel.Text = "Files were uploaded successfully.";

然而,它会说&#34;开始上传&#34;完成之后。这不好!因此,使用客户端脚本来设置&#34;开始上传...&#34;文本。

答案 2 :(得分:1)

您没有看到该文本的原因是您从未向用户发送信息。

基本上,这个过程是这样的:

  • 您的请求进入服务器
  • 服务器处理请求并创建所有HTML文本(此时方法已完全运行,因此YoutubeStatusLabel.Text将是您在UploadPromoFilesButton_Click方法中设置的最后一项内容
  • 将HTML文本发送回客户端

如果要显示中间文本,则需要在客户端(JavaScript)上设置值,或者对服务器进行多次回发。最后一个选项也需要JavaScript。

我会调查SignalR以获得更高级的客户端服务器通信。

答案 3 :(得分:1)

由于您的代码在服务器上执行且尚未返回到浏览器,因此您不会看到YoutubeStatusLabel.Text的第一次更新。

值得阅读Page Lifecycle

我没有看到通知用户上传已经开始然后在5秒后再次获益的好处。我将文本设置为Starting upload...,然后进行回调,一旦过程完成,就将标签更新为Files were uploaded successfully.。见here