This is a migrated thread and some comments may be shown as answers.

Notification text does not get updated

2 Answers 138 Views
Notification
This is a migrated thread and some comments may be shown as answers.
Yuriy
Top achievements
Rank 1
Yuriy asked on 03 Nov 2011, 02:41 PM
I am using RadNotification control to show the results of sending emails.

Emails are being sent asynchronously and I want to trigger a notification on the async callback. When I try to change the notification text from this function, it does not change it on the notification, showing the initially set message.

Any suggestions? Here is my c# code:

      protected void SendEmail(string Subject, string MessageBody, string EmailList)
        {
             MailMessage mail = new MailMessage();
            //set the addresses
             string SelectedEmails = "";
            mail.From = new MailAddress("noreply@domain.com");
            string[] Emails = EmailList.Split(';');
            foreach (string Email in Emails)
            {    //remove duplicates
                if (SelectedEmails.IndexOf(Email) == -1)
                {  mail.To.Add(Email);
                    SelectedEmails +="; "+ Email;}
             }

            //set the content
            mail.Subject = Subject;
            mail.Body = MessageBody;
            //send the message
            SmtpClient smtp = new SmtpClient();
            Object userState = mail;
           //Attach event handler for async callback
            smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
            //Send the email asynchronously
            smtp.SendAsync(mail, userState);
        }


        protected void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //Get UserState as MailMessage instance from SendMail()
            MailMessage mailMessage = e.UserState as MailMessage;
            if (e.Cancelled)
            {
                ShowNotification("Sending of email message was cancelled. Address=" + mailMessage.To[0].Address);
            }

            if (e.Error != null)
            {
                ShowNotification("Error occured, info=" + e.Error.Message);
            }
            else
            {
                string EmailRecepients = "";
                int count = mailMessage.To.Count;
                for (int i = 0; i < count; i++)
                {
                    EmailRecepients += "\r\n" + mailMessage.To[i].Address;
                }
                ShowNotification("Notification was sent successfully to:" + EmailRecepients);
            }
        }

        protected void ShowNotification(string NotifcationText)
            {
                RadNotification1.Text = NotifcationText;
                RadNotification1.Show();
            }

2 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 04 Nov 2011, 02:35 PM
Hi Yuriy,

You do not update the notification in this AJAX request so properties set to it will not be sent to the client. This is the general way AJAX works. The server-side Show() method injects a JavaScript function to show the notification and this is why you can see it show. What I advise is that you either include the notification in the update, or register a JavaScript function to change the text of the notification via its client-side API.

Please also note that the Text property takes an HTML string, so new lines should be <br /> instead of \r\n. Also calling the same method many times will not result in many notifications being shown, but one with the last text you provide, so I suggest you have your method built up the errors/messages you need to show (by appending them to a string) and then only once set it to the Text of the notification and call its Show() method.


All the best,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Yuriy
Top achievements
Rank 1
answered on 04 Nov 2011, 02:57 PM
Hi Martin,

Thanks for the quick reply. I included RadNotification control in the update (through AJAX Manager) and it works great now. Thanks for the other tips too :)

Regards,
Yuriy
Tags
Notification
Asked by
Yuriy
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Yuriy
Top achievements
Rank 1
Share this question
or