简体   繁体   English

在asp.net c#中延迟显示消息一段时间

[英]Delay display message by some time in asp.net c#

I have a label, all what I want is, to display label as "name1" then wait for 5 seconds and then change it to "name2".我有一个标签,我想要的只是将标签显示为“name1”,然后等待 5 秒钟,然后将其更改为“name2”。 This is how I did that我就是这样做的

protected void Page_Load(object sender, EventArgs e)
{
   Label1.Text = "name1";
   System.Threading.Thread.Sleep(5000);
   Label1.Text = "name2";
}

What it does is it wait's for 5 seconds in total and then display "name2".它的作用是总共等待 5 秒,然后显示“name2”。 "name1" is not displaying as I wished. “name1”没有按照我的意愿显示。 I tried these links,我试过这些链接,

How do I get my C# program to sleep for 50 msec? 如何让我的 C# 程序休眠 50 毫秒?

How to trigger a timer tick programmatically? 如何以编程方式触发计时器滴答?

Did not help.没有帮助。 This Using the ASP.NET Timer Control with Multiple UpdatePanel Controls seems to work but this is keep on refreshing the page.将 ASP.NET 计时器控件与多个 UpdatePanel 控件一起使用似乎有效,但这会不断刷新页面。 I don't want this to happen, it should display name1 then wait for 5 seconds, display name2 and then stop.我不希望这种情况发生,它应该显示 name1 然后等待 5 秒,显示 name2 然后停止。

您提到的更改必须在客户端,使用 javascript setTimer 方法并将标签更新为您需要的任何文本。

You can use Timer control in your page under UpdatePanel for Async functionality (for help on AJAX in ASP.net below are the links provided) , and then under its event use this logic for label name change:您可以在 UpdatePanel 下的页面中使用 Timer 控件来实现异步功能(下面提供了有关 ASP.net 中 AJAX 的帮助,提供了链接) ,然后在其事件下使用此逻辑更改标签名称:

 protected void Page_Load(object sender, EventArgs e)
 {
    Timer1.Enabled = true;
    Timer1.Interval = 5000;
 }

 protected void Timer1_Tick(object sender, EventArgs e)
 {
    if(Label1.Text == "name2")
    {
        Label1.Text = "name1";
    }
    else
    {
        Label1.Text = "name2";
    }
 }

for more help on this you can check here and here如需更多帮助,您可以在此处此处查看

You cannot handle this the way you have in ASP.NET since you will always get the end result from the form_load event.您无法像在 ASP.NET 中那样处理此问题,因为您将始终从 form_load 事件中获得最终结果。 You will have to use JavaScript timing events to handle this at the client side.您将不得不使用JavaScript 计时事件在客户端处理此问题。

      <script>
                $(document).ready(function(){
                 $('#label').val('label1');
                 setTimeout(SetLabel2, 5000); //wait for 5 seconds before setting the label. NOTE: This will keep on repeating until you clear the timeout
                });

                function SetLabel2(){
                  var l = $('#label).val();
                  if(l == 'label2)
                  {
                     window.clearTimeOut(); //So that this does not repeat
                  }
       </script>

As suggested by other contributors, you should use client code to obtain that kind of behavior.正如其他贡献者所建议的那样,您应该使用客户端代码来获得这种行为。 In your code sample, the Page_Load function simply takes 5 seconds to execute, and when all the processing on the server is done, the resulting HTML is finally sent to the browser... and you see "name2".在您的代码示例中, Page_Load函数只需 5 秒即可执行,当服务器上的所有处理完成后,生成的 HTML 最终会发送到浏览器……您会看到“name2”。

Here is a jQuery version of the client code:这是客户端代码的 jQuery 版本:

$(document).ready(function () {
    $('#Label1').html("name1");
    setTimeout(function () { $('#Label1').html("name2"); }, 5000);
})

and here is a pure Javascript version:这是一个纯 Javascript 版本:

var lbl1 = document.getElementById('Label1');
lbl1.innerHTML = "name1";
setTimeout(function () { lbl1.innerHTML = "name2" }, 5000);

If the ID of the Label is mangled by ASP.NET, you can use <%= Label1.ClientID %> instead of Label1 in the client code.如果 Label 的 ID 被 ASP.NET 弄乱了,您可以在客户端代码中使用<%= Label1.ClientID %>而不是Label1

This could be done using only what is provided, it should run once, so once the tick is complete we disable the timer.这可以仅使用提供的内容来完成,它应该运行一次,所以一旦滴答完成,我们就禁用计时器。

protected void Page_Load(object sender, EventArgs e)
{
    Timer1.Enabled = true;
    Label1.Text = "name1";
    Timer1.Interval = 2000;
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = "name2";
    Timer1.Enabled = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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