2011-08-04 83 views
0

我面臨着一個問題。基於事件的Asp.net更新標籤

我必須做的是。頁面上有一個按鈕,用於開始處理一些作業。

當我按下開始按鈕時,我正在調用業務層的ProcessJobs方法,該方法引發了一個具有總計作業數的事件。我必須在ListBox控件中顯示此計數。

在處理完作業後,通過同一個事件產生了幾個狀態消息。這些消息應顯示在列表框控件中的網頁表單

但我沒有獲取網頁來自行更新。我還添加了定時器控制並在1秒後刷新頁面。但頁面未被更新。

這裏是代碼

Default.aspx.cs

public partial class xxxxx: System.Web.UI.Page 
{ 
    private xxxx manager; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (this.manager == null) 
     { 
      manager = new xxxx(); 
      this.manager.ProcessEmailEvent += new EventHandler<EventArg<string>>(manager_ProcessEmailEvent); 
     } 

     if (!Page.IsPostBack) 
     { 
      this.btnStop.Visible = false; 

     } 
    } 

    void manager_ProcessEmailEvent(object sender, EventArg<string> e) 
    { 
     this.lblStatus.Text = this.lblStatus.Text + "<br>" + e.Data; 
    } 

    protected void processEmailTimer_Tick(object sender, EventArgs e) 
    { 
     int tickCount = this.manager.EmailProcessCount; 
     int totalEmailCount = this.manager.TotalEmailsCount; 

     this.lblProcessEmail.Text = string.Format("Processing Emails <b>{0}</b> of <b>{1}</b>", tickCount, totalEmailCount); 

    } 
    protected void btnStart_Click(object sender, EventArgs e) 
    { 
     this.processEmailTimer.Enabled = true; 
     this.btnStart.Visible = false; 
     this.btnStop.Visible = true; 
this.manager.ProcessEmails(this.chkValidateEmail.Checked, this.chkTesting.Checked); 

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

     this.processEmailTimer.Enabled = false; 
     this.btnStart.Visible = true; 
     this.btnStop.Visible = false; 
     this.manager.Stop(); 
    } 

這裏是業務邏輯處理電子郵件

public event EventHandler<EventArg<string>> ProcessEmailEvent; 

    public void InvokeProcessEmailEvent(EventArg<string> e) 
    { 
     EventHandler<EventArg<string>> handler = ProcessEmailEvent; 
     if (handler != null) handler(this, e); 
    } 

public void ProcessEmails(bool validateSenderEmail, bool deleteEmailAfterProcess) 
    { 
     // Creaate pop3 Client 
     POP3_Client popClient = new POP3_Client(); 

     // Connect to POP3 host 
     popClient.Connect(Utility.POP3Host, Utility.POP3Port, Utility.UseSSL); 

     // Authenticate email address and password 
     popClient.Authenticate(Utility.EmailAddress, Utility.Password, false); 

     // Get Email count 
     int msgCount = popClient.Messages.Count; 

// Raising event to update status on default.aspx 

     this.InvokeProcessEmailEvent(new EventArg<string>("Emails : " + msgCount)); 

     TotalEmailsCount = msgCount; 


     // Start emai Processing 
     processEmailStarted = true; 

     for (int i = 0; i < msgCount; i++) 
     { 
      this.EmailProcessCount = i + 1;   

// Raising event to update status on default.aspx 
      this.InvokeProcessEmailEvent(new EventArg<string>("Getting Email " + (i + 1))); 

      // Get message 
      var msg = popClient.Messages[i]; 

      // Get Email header 
      Mail_Message mime = Mail_Message.ParseFromByte(msg.HeaderToByte()); 

      StringBuilder sb = new StringBuilder(); 

      this.InvokeProcessEmailEvent(new EventArg<string>("--------------------------------")); 


      this.InvokeProcessEmailEvent(new EventArg<string>("From : " + mime.From.ToString())); 


      this.InvokeProcessEmailEvent(new EventArg<string>("Subject : " + mime.Subject.ToString())); 



      this.InvokeProcessEmailEvent(new EventArg<string>("Date : " + mime.Date.ToString())); 


      this.InvokeProcessEmailEvent(new EventArg<string>("Size (KB): " + (msg.Size/(decimal)1000).ToString()));   

     } 
    } 
+0

你可以分享你的代碼,所以我們可以看到到底是怎麼回事呢? – Becuzz

+0

您想在更新面板中更改此消息嗎?你的aspx頁面是什麼樣的? – Becuzz

+0

是有更新面板和按鈕,列表框和定時器在更新面板中 – Mohsan

回答

0

你必須改變你的這些事件是如何被提出的基本方法。高位在該列表上會讓經理成爲一個生活在頁面之外的單身人士。你有你的代碼的方式,管理器對象實例化每個頁面請求(包括計時器滴答)。看看這篇文章很長的流程

http://msdn.microsoft.com/en-us/magazine/cc163553.aspx

+0

任何運行的例子???如何更新答案,而無需重新加載頁面? – Mohsan