Notification text does not get updated

3 posts, 0 answers
  1. Yuriy
    Yuriy avatar
    13 posts
    Member since:
    Jul 2011

    Posted 03 Nov 2011 Link to this post

    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. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3600 posts

    Posted 04 Nov 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Yuriy
    Yuriy avatar
    13 posts
    Member since:
    Jul 2011

    Posted 04 Nov 2011 Link to this post

    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
Back to Top