Asp.net根据事件更新标签

时间:2011-08-04 16:22:10

标签: asp.net

我正面临一个问题。

我要做的是。页面上有一个按钮,可以开始处理某些作业。

当我按下开始按钮时,我正在调用我的业务层的ProcessJobs方法,该方法会引发具有总工作数的事件。我必须在ListBox控件中显示此计数。

在处理作业之后,通过同一事件引发了几条状态消息。这些消息应以列表框控件中的Web表单显示

但我没有让网页更新自己。我还添加了计时器控制并在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()));           

        }
    }

1 个答案:

答案 0 :(得分:0)

您必须更改这些事件如何引发的基本方法。该列表中的高位将使经理成为居住在页面之外的单身人士。您拥有代码的方式,管理器对象在每个页面请求(包括计时器滴答)上实例化。查看有关长时间运行进程的这篇文章

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