显示“已保存”标签2秒钟,然后隐藏在c#asp.net中

时间:2014-04-09 09:00:45

标签: c# asp.net label

在保存数据之后显示label(包含text =已成功保存)的最佳方法是什么才能显示(可见=真)然后在说2秒后消失(变为可见=假)?我以前见过人们使用计时器但却无法让他们工作。

if (saved == true)
{
   //data saved - show label and then make visible = false
   lblsuccess.Visible = true;
   lblsuccess.Text = "Visit saved";
}

5 个答案:

答案 0 :(得分:2)

System.Timers.Timer timer1;


timer1 = new System.Timers.Timer(2000);
timer1.Enabled=false;
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            lblsuccess.Visible = false;
            timer1.Enabled=false;
        }

 if (saved == true)
            {
                //data saved - show label and then make visible = false

                timer1.Enabled=true;
                lblsuccess.Visible = true;
                lblsuccess.Text = "Visit saved";
             }

答案 1 :(得分:2)

在CS中使用此一行代码

默认情况下设置标签Visibility = False;

    ScriptManager.RegisterClientScriptBlock (this.Page, typeof (Page ), "script" , "window.setTimeout(function() { document.getElementById('" + lblSubMsg.ClientID + "').style.display = 'none' },2000);", true);

答案 2 :(得分:1)

找到了这个,它为我做了诀窍!感谢您上面的答案

  if (saved == true)
  {
        lblsuccess.Visible = true;
        lblsuccess.Text = "Visit saved";

        ClientScript.RegisterStartupScript(this.GetType(), "HideLabel", "<script type=\"text/javascript\">setTimeout(\"document.getElementById('" + lblsuccess.ClientID + "').style.display='none'\",2000)</script>");

  }

答案 3 :(得分:0)

试试这个:

if (saved == true)
            {
                //data saved - show label and then make visible = false    

                lblsuccess.Visible = true;
                lblsuccess.Text = "Visit saved";
                System.Threading.Thread.Sleep(2000);
                lblsuccess.Visible= false;
             }

答案 4 :(得分:0)

你需要通过java-script jquery delay函数来完成,如下所示
请注意,延迟是一个整数,表示延迟执行队列中下一个项目的毫秒数。

在jquery的doucment.ready函数中,您可以编写

$("#lblsuccess").delay(3200).fadeOut(300); 

或者您想使用客户端ID

 $("#<%=lblsuccess.ClientID %>")..delay(3200).fadeOut(300);