New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Specify order when loading images in RadImageGallery

HOW-TO

Change order when loading images in RadImageGallery.

DESCRIPTION

In order to change the default order of images by Name, Last Modified Date, Alphabetic descending or other, generate the DataSource records by cycling through the files in the specified images folder and sort them before loading the DataSource via the NeedDataSource event of RadImageGallery.

SOLUTION

The example below shows how to get the images from a folder named Images (placed in the root of the web app/site), sort the files by Creation Time in descending order, create and set the DataSource of the Image Gallery:

Default.aspx:

<%@ page language="C#" autoeventwireup="true" codefile="Default.aspx.cs" inherits="Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" />
        <telerik:RadImageGallery RenderMode="Lightweight" runat="server" ID="RadImageGallery1" DisplayAreaMode="Image" OnNeedDataSource="RadImageGallery1_NeedDataSource"
            Width="600px" Visible="true"
            DataTitleField="Title" DataDescriptionField="Description" DataImageField="ImageData" DataThumbnailField="ThumbnailData">
            <ImageAreaSettings Height="500px" />
        </telerik:RadImageGallery>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Default : System.Web.UI.Page
{

    protected void RadImageGallery1_NeedDataSource(object sender, Telerik.Web.UI.ImageGalleryNeedDataSourceEventArgs e)
    {
        RadImageGallery1.DataSource = LoadGallery();
    }

    private DataTable LoadGallery()
    {
        DirectoryInfo info = new DirectoryInfo(Server.MapPath("~/Images"));

        FileInfo[] files = info.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();

        //Data table for image gallery data source
        DataTable table = new DataTable();
        table.Columns.Add("Title", typeof(string));
        table.Columns.Add("Description", typeof(string));
        table.Columns.Add("ImageData", typeof(string));
        table.Columns.Add("ThumbnailData", typeof(string));

        foreach (FileInfo file in files)
        {
            table.Rows.Add(file.Name, "", "~/Images/"+ file.Name, "~/Images/" + file.Name);
        }

        return table;
    }
}
In this article
Not finding the help you need? Improve this article