2012-02-10 24 views
1

我想diplay秒錶計時...使用Asp.Net C#...秒錶計時在Asp.Net

我reffer多線程,並在谷歌搜索也... ,但無法找到正確的代碼或解決方案.. 最讓我找到了解決方案,通過點擊都很難implemnt和一些didnt的工作......

我想創建秒錶像下面圖片..

enter image description here

在這啓動按鈕從00.00開始手錶,格式爲HH.MM.

並將按鈕的文本更改爲停止。 並通過點擊一遍,使其停止手錶.. ,然後保存了當時的數據庫,並在給定的文本框通過點擊重置重置時間至00:00顯示.... ..

怎麼能我做這些?

我沒有關於this..so需要一些代碼的任何想法...

OK下面是代碼和圖片,我試圖網站...

enter image description here

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Diagnostics; 





public partial class Default5 : System.Web.UI.Page 
{ 
    private Stopwatch sw = new Stopwatch(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 


     } 

     protected void Button3_Click(object sender, EventArgs e) 
     { 
      if (Button3.Text == "start") 
      { 
       Timer1.Enabled = true; 
       sw.Start(); 
       Button3.Text = "stop"; 

      } 
      else 
      { 
       Timer1.Enabled = false; 
       sw.Stop(); 
       Button3.Text = "start"; 

       //TextBox1.Text = Label1.Text; 
      } 

     } 
     protected void Timer1_Tick(object sender, EventArgs e) 
     { 

      int hrs = sw.Elapsed.Hours; 
      //int hrs = "1; 
      int mins = sw.Elapsed.Minutes; 
      int secs = sw.Elapsed.Seconds; 

      Label1.Text = hrs + ":"; 

      if (mins < 10) 
       Label1.Text += "0" + mins + ":"; 
      else 
       Label1.Text += mins + ":"; 

      if (secs < 10) 
       Label1.Text += "0" + secs; 
      else 
       Label1.Text += secs; 



     } 

} 

,筆譯項目中的代碼(這WELL廠)...

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private Stopwatch sw = new Stopwatch(); 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (button1.Text == "Start") 
      { 
       timer1.Enabled = true; 
       sw.Start(); 
       button1.Text = "Stop"; 

      } 
      else 
      { 
       timer1.Enabled = false; 
       sw.Stop(); 
       button1.Text = "Start"; 
       textBox1.Text = label1.Text; 
      } 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      int hrs = sw.Elapsed.Hours, mins = sw.Elapsed.Minutes, secs = sw.Elapsed.Seconds; 

      label1.Text = hrs + ":"; 

      if(mins <10) 
       label1.Text += "0" + mins + ":"; 
      else 
       label1.Text += mins + ":"; 

      if (secs < 10) 
       label1.Text += "0" + secs; 
      else 
       label1.Text += secs; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 


    } 
} 

在這兩種定時器間隔被設定在1000

在網站上,該時間不增加。它仍然是0:00:00 ...

+1

你有在C#中的經驗嗎?你知道如何訪問數據庫嗎? – 2012-02-10 07:25:06

+0

是的,我嘗試使用內置設施來實現它,但它在項目(創建新項目)中運行良好,不能在網站工作(創建新網站)。 – Dip 2012-02-10 10:44:52

+0

如果您發佈的網站中的代碼不工作,以及某些細節不適用,那麼也許我們可以幫助:) – 2012-02-10 12:36:50

回答

1

開發桌面和Web應用程序有很大的不同。

在您的WinForm應用程序中,該應用程序在一臺電腦上運行,在一個進程中。後面的代碼可以持續訪問用戶界面,並可以更新當前時間。

Web application中,一切都不一樣。網絡本身就是一個斷斷續續的無狀態環境。這意味着服務器從世界某個地方的瀏覽器獲取請求,創建響應並將其發送回瀏覽器並忘記它。

這意味着瀏覽器中顯示的用戶界面和服務器上運行的代碼之間沒有永久連接。這就是爲什麼你的代碼不工作。

ASP.NET WebForms的功能可以幫助您解決Web開發中固有的問題,這種技巧會讓您相信存在某種永久連接。

我會建議,爲了更好地理解事情是如何工作的,從代碼中刪除計時器並添加一個按鈕,單擊它時會更改標籤文本。

要真正創建秒錶,您需要在用戶的瀏覽器中運行的代碼,而不是在服務器上運行。在瀏覽器中運行的代碼知道環境和用戶界面,並且可以相應地更新事物。這是用一種不同的編程語言JavaScript完成的。

我建議先從教程here開始。

+0

嘿,先生wouter de Kort,感謝您給我這些知識和信息..我將從您建議的鏈接中瞭解JavaScript和Web應用程序。是的,我使用其他邏輯實現了StopWatch ...並感謝大家的建議..: ) – Dip 2012-02-11 10:56:24

0

您似乎完全混淆了單層有狀態桌面應用程序和雙層無狀態Web應用程序。你不能只在一臺服務器上運行代碼,它會奇蹟般地更新,就像它是一個沒有很多複雜的Ajax和jQuery技巧的Windows窗體。請閱讀有關HTTP,Web服務器和Web瀏覽器實際工作方式的更多信息。

1

嘗試下面的代碼。這個對我有用。

添加下面的代碼在websource代碼:

<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
    <asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label> 
    <asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" /> 
    </ContentTemplate> 
    <Triggers> 
    <asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" /> 
    </Triggers> 
</asp:UpdatePanel> 

添加下面的源代碼在你的CS文件:

using System.Diagnostics; 

public partial class ExamPaper : System.Web.UI.Page 
{ 
    public static Stopwatch sw; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      sw = new Stopwatch(); 
      sw.Start(); 
     } 
    } 

    protected void tm1_Tick(object sender, EventArgs e) 
    { 
     long sec = sw.Elapsed.Seconds; 
     long min = sw.Elapsed.Minutes; 

     if (min < 60) 
     { 
      if (min < 10) 
       Label1.Text = "0" + min; 
      else 
       Label1.Text = min.ToString(); 

      Label1.Text += " : "; 

      if (sec < 10) 
       Label1.Text += "0" + sec; 
      else 
       Label1.Text += sec.ToString(); 
     } 
     else 
     { 
      sw.Stop(); 
      Response.Redirect("Timeout.aspx"); 
     } 
    } 
}