Articles in this section
Category / Section

How to create Rotator in code behind?

1 min read

You can create Rotator and Rotator items entirely in code behind. PlaceHolder holds the Rotator control. An object is created for the Rotator control. Height and Width properties are set by SlideWidth and SlideHeight to the slider. There are two methods to bind Rotator items. Rotator items are created by using either Data source or Item template. In Data source, items are created as a list and they are directly mapped to Rotator. But in Item template each and every item is added separately. Refer the following code example.

ASPX

<asp:PlaceHolder ID="holdrotator" runat="server"></asp:PlaceHolder>
<asp:PlaceHolder ID="holdrotator2" runat="server"></asp:PlaceHolder>

 

C#

  protected void Page_Load(object sender, EventArgs e)
        {
protected void Page_Load(object sender, EventArgs e)
        {
            //Rotator with DataBinding to as list
            Rotator myRotator = new Rotator();
            myRotator.ID = "Sample";
            myRotator.SlideWidth = "600px"; // Set the rotator slide width in pixels
            myRotator.SlideHeight = "350px"; // Set the rotator slide height in pixels
            myRotator.DataSource = new Rotate_items().GetItems().ToList();  // create a datasource with rotator items and bind them as list
            // Map the data fields with corresponding name in the datasource
            myRotator.DataCaptionField = "Caption"; //set the field name to display caption on mouseover
            myRotator.DataUrlField = "Url"; // set the field name to display images in rotator
            //Rotator creation using ItemTemplate
            Rotator myRotator2 = new Rotator();
            myRotator2.ID = "Sample1";
            myRotator2.SlideWidth = "500px";
            myRotator2.SlideHeight = "250px";
            //Intialize rotator items to be added
            RotatorItem first = new RotatorItem();
            RotatorItem second = new RotatorItem();
            RotatorItem third = new RotatorItem();
            RotatorItem forth = new RotatorItem();
            //Set the image url for every rotator item
            first.Url = "../images/rotator/green.jpg";
            second.Url = "../images/rotator/nature.jpg";
            third.Url = "../images/rotator/seaview.jpg";
            forth.Url = "../images/rotator/sculpture.jpg";
            //Add the rotator item to the Rotator control
            myRotator2.Items.Add(first);
            myRotator2.Items.Add(second);
            myRotator2.Items.Add(third);
            myRotator2.Items.Add(forth);
            //Add the rotator control to the placeholder in aspx page
            holdrotator2.Controls.Add(myRotator2);
            holdrotator.Controls.Add(myRotator);
        }    
} 
public class Rotate_items
    {
        public Rotate_items() { }
        public Rotate_items(string _caption, string _url)
        {
            this.Caption = _caption;
            this.Url = _url;
        }
        public string Caption { get; set; }
        public string Url { get; set; }
        public List<Rotate_items> GetItems()
        {
            List<Rotate_items> data = new List<Rotate_items>();
            data.Add(new Rotate_items("Humming", "../images/rotator/bird.jpg"));
            data.Add(new Rotate_items("Flower", "../images/rotator/rose.jpg"));
            data.Add(new Rotate_items("Winter", "../images/rotator/snowfall.jpg"));
            data.Add(new Rotate_items("Night", "../images/rotator/night.jpg"));
            return data;
        }
    }                      

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied