How do I add items to a rotator programmatically?

2 posts, 0 answers
  1. Jeff
    Jeff avatar
    7 posts
    Member since:
    Jul 2011

    Posted 23 May 2012 Link to this post

    Can someone tell me why this code doesn't work? Checked the demos and documentation but didn't find an answer and the forum related to this topic are very old.

    <telerik:RadRotator ID="rotator1" runat="server" Width="800px" Height="150px" SlideShowAnimation-Type="Fade" SlideShowAnimation-Duration="500" ItemHeight="63" ItemWidth="125" RotatorType="Buttons" OnClientItemShown="OnClientItemShown"></telerik:RadRotator>
    var item = new Telerik.Web.UI.RadRotatorItem();
    item.Controls.Add(new LiteralControl("<div>1</div>"));
    rotator1.Items.Add(item);
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 May 2012 Link to this post

    Hi Jeff,

    As far as I know, a control can added only to an ItemTemplate. Since you are adding a LiteralControl dynamically, you must define a custom class that implements the ITemplate interface. Here is the sample code.

    ASPX:
    <telerik:RadRotator ID="RadRotator1" runat="server" Width="800px" Height="150px" SlideShowAnimation-Type="Fade" SlideShowAnimation-Duration="500" ItemHeight="63" ItemWidth="125" RotatorType="Buttons" DataSourceID="SqlDataSource1">
    </telerik:RadRotator>

    C#:
    public partial class exercise_rotator : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            RadRotator1.ItemTemplate = new MyItemTemplate();
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
    internal class MyItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            LiteralControl ltl = new LiteralControl("<div>1</div>");
            container.Controls.Add(ltl);
        }
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top