Learning
Learning

Reputation: 20001

Refresh UpdatePanel area every 2 seconds from code behind

I am working on a small newsletter application which can send newsletter to small number of users let us say between 100- 200 users.

I want admin to see the progress of newsletter broadcast every 2-3 seconds by displaying the counter & adding email address to the listbox.

I am trying to achieve this with the following code. I have added partial code. It works fine but show result after sending email to all the user.

 <asp:UpdatePanel ID="updPanelNewsletterProgress" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
            <asp:Label ID="lblCounter" runat="server" Text=""></asp:Label><br /><br />
            <asp:CheckBoxList ID="cbListEmailsAddress" runat="server">
            </asp:CheckBoxList>
    </ContentTemplate>
</asp:UpdatePanel>  


protected void btnSendNewsletter_Click(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    string strSql = "SELECT DISTINCT(Email) FROM Subscribe WHERE unsubscriber=0";
    int counter, totalRows;
    counter = 0;
    ds = DataProvider.Connect_Select(strSql);
    totalRows = ds.Tables[0].Rows.Count;
    DataTable dt = ds.Tables[0];

    foreach (DataRow dRow in dt.Rows)
    {
        to = ds.Tables[0].Rows[0]["Email"].ToString();
        Helper.SendEmailNewsletter(to, subject, message, isHtml);
        counter = counter + 1;
        lblCounter.Text = "Sending " + counter.ToString() + " of " + totalRows.ToString();
        updPanelNewsletterProgress.Update();
        System.Threading.Thread.Sleep(2000);
    }

}

I would appreciate if someone can point me to a more correct approach & show i can update the count while from code behind while sending email.

Example : Sending 1 of 100 sending 10 of 100

Upvotes: 0

Views: 1912

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

Using Thread.Sleep() or a Timer in code-behind won't help you.

ASP.NET is a Request/Response model and the UpdatePanel does partial page updates but also follows this Request/Response model.

You can use the (Client side) Timer that's on the same tab as the UpdatePanel.

Upvotes: 1

Related Questions