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

问题描述 投票:1回答:5

将数据保存为仅可见(可见=真),然后在说2秒钟后消失(变为可见=假)后显示label(包含文本=保存成功)的最佳方法是什么?我见过人们以前使用计时器,但无法使它们工作。

if (saved == true)
{
   //data saved - show label and then make visible = false
   lblsuccess.Visible = true;
   lblsuccess.Text = "Visit saved";
}
c# asp.net label
5个回答
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";
             }

2
投票

在CS中使用这一行代码默认情况下设置标签Visibility = False;

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

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>");

  }

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;
             }

0
投票

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

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

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

或您要使用客户端ID,然后使用

 $("#<%=lblsuccess.ClientID %>")..delay(3200).fadeOut(300); 
© www.soinside.com 2019 - 2024. All rights reserved.