Creating RadNotificacion and rich content (ContentTemplate) dynamically

4 posts, 1 answers
  1. Nefi
    Nefi avatar
    2 posts
    Member since:
    Jul 2012

    Posted 24 Jul 2012 Link to this post

    Hi everyone,

    I'm having an issue trying to add rich content to a RadNotification created dynamically. I'm developing ASP.NET Composite Server Controls that nests Telerik controls and it is imposible to add any control to the RadNotification's ContentTemplate/ContentContainer. This is my code:

    protected override void CreateChildControls()
    {
        var _notification = new RadNotification
        {
            ID = "Notification",
            Position = NotificationPosition.Center,
            Height = Unit.Pixel(160),
            Width = Unit.Pixel(330),
            TitleIcon = "ok",
            Animation = NotificationAnimation.Slide,
            AutoCloseDelay = 0,
            ShowCloseButton = false,
        };
        var div = new HtmlGenericControl("div");
        div.Controls.Add(new LiteralControl("<br />"));
        div.Controls.Add(new Literal { Text = Resources.MyControl_TextMessage });
        div.Controls.Add(new LiteralControl("<br />"));
        div.Controls.Add(new RadButton { Text = Resources.MyControl_ButonText });
        _notification .ContentContainer.Controls.Add(div);
        this.Controls.Add(_notification);
    }

    BTW, this is not the full method, full method contains a lot of more controls, this is just a sample.

    Hope anyone knows how can I achieve it.

    Thanks,
    Nefi
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Jul 2012 Link to this post

    Hi Nefi,

    With reference to this documentation "If you are planning to add controls dynamically you need to declare an empty ContentTemplate, since if it is not present RadNotification automatically detects it and enters into simple content mode and you will not be able to add controls in it." Following is the sample code that I tried to add controls dynamically to RadNotification.

    C#:
    RadNotification _notification = new RadNotification();
    protected override void OnInit(EventArgs e)
     {
       _notification.ContentTemplate = new NotificationTemplate();
       base.OnInit(e);
     }
    protected void Page_Load(object sender, EventArgs e)
     {
       _notification.ID = "Notification";
       _notification.Position = NotificationPosition.Center;
       _notification.Height = Unit.Pixel(160);
       _notification.Width = Unit.Pixel(330);
       _notification.TitleIcon = "ok";
       _notification.Animation = NotificationAnimation.Slide;
       _notification.AutoCloseDelay = 0;
       _notification.ShowCloseButton = false;
       _notification.VisibleOnPageLoad = true;
       var div = new HtmlGenericControl("div");
       div.Controls.Add(new LiteralControl("<br />"));
       div.Controls.Add(new Literal { Text = "ffggh" });
       div.Controls.Add(new LiteralControl("<br />"));
       div.Controls.Add(new RadButton { Text = "tftyh" });
       _notification.ContentContainer.Controls.Add(div);
       form1.Controls.Add(_notification);
     }
     
    //creating an empty ContentTemplate
     
    class NotificationTemplate : ITemplate
     {
       public void InstantiateIn(Control container)
        {
        }
     }

    Hope this helps.

    Regards,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Nefi
    Nefi avatar
    2 posts
    Member since:
    Jul 2012

    Posted 25 Jul 2012 Link to this post

    Yeah, It worked like a charm!

    Thank you very much,
    Nefi
  5. Meera
    Meera avatar
    14 posts
    Member since:
    Mar 2012

    Posted 09 Oct 2014 Link to this post

    I would like to use the above method to create Notifications but slightly different. In my case, I would like to create multiple notifications based on database records. The database records contain the notification text, interval etc and multiple notifications need to be created dynamically based on the number of records. Could you suggest a modification for the above answer to fit my criteria? 
Back to Top