Click here to Skip to main content
15,887,214 members
Articles / Web Development / HTML
Tip/Trick

Bind Image Slider With a Folder (Without Database) in ASP.NET C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (12 votes)
16 Sep 2014CPOL1 min read 20.2K   700   11   2
Bind an image slider without database in ASP.NET

Introduction

Here in this tip, I will show you how to bind an image slider with a folder, so that you don't need to create database for a slider. Just put images or replace images to change slider.

Using the Code

So how to do this? I have taken a repeater to show the slider. What actually does a slider hold? A bunch of li within ul. So place one li into the repeater to Repeater the image slider. And behind the binding logic, I discovered the images within a directory (or folder) and put the image path within a List. Bind the list with the repeater.

Put all the resources into your solutions like the CSS, JS, images into your Solution Explorer. Let's look at how the image slider was placed in HTML.

HTML
<div id="banner-fade"><img title="title1" 
src="img/banner01.jpg" />
<img title="title1" src="img/banner01.jpg" />
<img title="title1" src="img/banner01.jpg" />
<ul class="bjqs">
    <li><img title="title1" src="img/banner01.jpg" /></li>
</ul>
</div>

Now let's look at how I change the li with the repeater.

HTML
<div id="banner-fade">
<ul class="bjqs">
    <li><asp:Repeater ID="Repeater1" runat="server"> <itemtemplate /></li>
    <li><img title="<image title>" src="<the image source>" /></li>
</ul>
</div>

Change the repeater name according to your name.

Everything up to now is about designing. Now let's come to the code directly. Here, I get all the file names within a particular folder and then store those into the List and then bind the repeater with the List. Check its code.

C#
string[] filePaths = Directory.GetFiles(Server.MapPath("~/img/"));
List<listitem> files = new List<listitem>();
foreach (string filePath in filePaths)
{
    string fileName = Path.GetFileName(filePath);
    files.Add(new ListItem(fileName, "img/" + fileName));
}
Repeater1.DataSource = files;
Repeater1.DataBind();

Place the code into the Page_Load of your page to start the slider. Here, I have bind with the img folder. Replace the name according to your project.

Points of Interest

Here your slider is binding with the folder, so there are no more issues to write the SQL queries or any update delete adding of images. As simple as that! You can also bind other things similar to slider like accordion, carousel, etc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
QuestionAsp.net slider Pin
Member 1331364823-Oct-17 16:34
Member 1331364823-Oct-17 16:34 
AnswerRe: Asp.net slider Pin
Arkadeep De5-Dec-17 23:25
professionalArkadeep De5-Dec-17 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.