简体   繁体   English

如何使用asp:timer来计时按钮单击事件中发生的动作?

[英]How do I use a asp:timer to time actions happening inside a button click event?

I have an application that allows a user to save a record to the database. 我有一个允许用户将记录保存到数据库的应用程序。 When The user clicks save I want to show a label that says Saving record please wait... then after 3 seconds I want the label to say Record Saved . 当用户单击“保存”时,我想显示一个标签为“正在保存记录”,请稍候...然后三秒钟后,我希望标签显示“已保存记录” Finally after 3 more seconds I want to go back to the normal view. 最后,再过3秒钟后,我想回到普通视图。 Here is hat I tried: 这是我尝试过的帽子:

txtSaveReportAs.Enabled = false;
strSaveNotify = "lblWarning";
lblSaveAs.Text = "Saving Your Data. Please Wait... ";
System.Windows.Forms.Timer Timer1 = new System.Windows.Forms.Timer();
Timer1.Interval = 3000;
Timer1.Start();
strSaveNotify = "lblSuccess";
lblSaveAs.Text = "Data Saved Successfully";
Timer1.Stop();
Timer1.Interval = 3000;
Timer1.Start();
txtSaveReportAs.Enabled = true;
txtSaveReportAs.Text = string.Empty;

When I do this it cancels out my sqlCommand so txtSaveReporAs just does not become disabled. 当我这样做时,它将取消我的sqlCommand因此txtSaveReporAs不会被禁用。 Also, strSaveNotify and lblSaveReport as does not change and no info gets saved to the database. 另外,strSaveNotify和lblSaveReport as不会更改,也不会将任何信息保存到数据库。 How is this done? 怎么做?

You are using the Timer like a Sleep, which halts execution within the current thread - not that I am suggesting you replace it with a Sleep, because they should only be used in limited circumstances and this is not one. 您使用的计时器就像睡眠一样,它将在当前线程中暂停执行-并不是建议您将其替换为睡眠,因为它们只应在有限的情况下使用,而不是一个。

For the Timer to work it needs an Tick event handler added to it. 为了使Timer工作,需要向其中添加一个Tick事件处理程序。 The event handler gets triggered when when the timer has completed the 3 seconds. 当计时器完成3秒后,将触发事件处理程序。 You don't call Stop() in the same set of code, because this will turn it off immediately after you have turned it on. 您不要在同一组代码中调用Stop(),因为打开后会立即将其关闭。 Processing doesn't stop and wait where you have Timer1.Start(), it keeps going. 处理不会停止并等待Timer1.Start()的继续进行。

You need to look up the specifications for the Timer. 您需要查找计时器的规格。

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.tick(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.windows.forms.timer.tick(v=vs.110).aspx

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

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