Revamping the images within a website's gallery layout

I've been struggling with my problem for a week now and can't seem to figure it out. I'm looking to create a webpage that will scroll through 5 or 6 images by clicking a next or prev button in Visual Studio Web Developer. Can someone guide me on how to achieve this from scratch?

I have a blank website created with a master page in Visual Studio, and I want to add this gallery function to the auto-generated Default.aspx file.

The image names are pic001.jpg; pic002.jpg; pic003.jpg and so on.

All I need is a previous button on the left to go back to the previous image, a next button on the right to show the next picture, and the changing image displayed in the middle.

Please assist me with this issue as I've attempted and failed miserably at it. Thank you to anyone who can help!

Here is some of the code from my attempts:

Default.aspx file

<%@ Page Title="Default" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"  %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server" >
    <script src="myJava.js" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server" >
<table>
    <tr><td> GALLERY </td></tr> <!--Header-->
    <tr>
        <td> <asp:Button ID="Button1" runat="server" Text="Prev" OnClientClick="getPrevImage()"/> </td>
        <td> <img ID="pic" alt="" src="" runat="server" width="400" height="400" /> </td>
        <td> <asp:Button ID="Button2" runat="server" Text="Next" OnClientClick="getNextImage()"/> </td>
    </tr>
</table>
</asp:Content>

Default.aspx.cs file

public partial class Default : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string script = string.Empty;
            script += "<script language='javascript'>";

            script += "init()";
            script += "</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", script);
        }
    }
}

myJava.js file

var imagePath = new Array();
var imageIndex = 0;

function init(){
     addPath("pic001.jpg");
     addPath("pic002.jpg");
     addPath("pic003.jpg");
     addPath("pic004.jpg");
     addPath("pic005.jpg");

     getImagePath(0);
}

function addPath(path){
     var index = imagePath.length;
     imagePath[index++] = path;  
}

function getImagePath(index){
     var length = imagePath.length;

     if(index <= length){
        if(index >= 0){
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
            imageIndex = index;
        }
     } else {
        document.getElementById("MainContent_pic").src = "DOES NOT EXIST";
        document.getElementById("MainContent_pic").alt = "DOES NOT EXIST";
     }
}

function getNextImage(){
     var length = imagePath.length;
     var index = imageIndex;
     if(index++ < length--){
        if(imagePath[index] != null){
            imageIndex = index;
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
        }                                    
     }
}

function getPrevImage(){
     var index = imageIndex;
     if(index-- >= 0){
        if(imagePath[index] != null){
            imageIndex = index;
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
        }   
     }
}

Answer №1

Below is a simple method using HTML and jQuery that you can easily implement into your application:

HTML (you can replace <a> with any tag of your choice):

<a href="" class="previous">Previous</a>
<img src="1.jpg" class="display"/>
<img src="2.jpg" class="hide"/>
<img src="3.jpg" class="hide"/>
<img src="4.jpg" class="hide"/>
<a href="" class="next">Next</a>

jQuery:

$(document).ready(function(){
    $(.previous).click(function(){
        var current = $(.display);
        //hide current image
        current.attr("class", "hide");
        //get previous image
        var pre = current.pre();
        if(pre !== null)
        {
            //display previous image if available
            pre.atrr("class", "display");
        }
        else
        {
            //display last image if previous not available
            current.parent().find("img:last").attr("class", "display");
        }
    });

    $(.next).click(function(){
        var current = $(.display);
        //hide current image
        current.attr("class", "hide");
        //get next image
        var next = current.next();
        if(next !== null)
        {
            //display next image if available
            next.atrr("class", "display");
        }
        else
        {
            //display first image if next not available
            current.parent().find("img:first").attr("class", "display");
        }
    })
});

CSS (you can adjust the display: block to suit your needs):

img.display {
    display: block;
}

img.hide {
    display: none;
}

I'm not an expert in HTML, CSS, or jQuery, so I hope this information is helpful to you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating a zipped collection within a server's memory and sending it back through a web API

Struggling to create a zip archive and deliver it through my web API to an Angular 2 site. Despite successfully creating the zip file, it always shows up as invalid when opened. Initially, I used using statements for the streams, but faced disposal issues ...

Is the PHP Ajax parameter missing during the upload process?

I'm attempting to do a simple upload, but I seem to be struggling. It could be that I'm not understanding it properly, or perhaps it's just too late at night for me to figure it out. After doing some research, I came across this example on ...

Using jQuery, you can activate an onclick function based on specific keywords entered by the user

Within this element, there is a text input field that allows users to search for specific items. Next to the input field is an icon image that can be clicked on to trigger a jQuery onclick event. This event sends an AJAX request to a PHP file, retrieves da ...

Nodejs is encountering difficulties in transmitting precise error messages to the browser

I am encountering an issue in my nodejs application with error handling. Despite setting up specific error messages, the browser receives a generic error object instead. The error messages are only visible in the nodejs console and not on the browser which ...

Whenever I try to run npm start, my webpack is not able to locate my index.html page

Recently delving into the world of node.js and its packages, I initiated by executing npm init to lay out the package.json structure shown below: { "name": "test", "version": "1.0.0", "description": & ...

Utilize data from API responses to configure settings in angular-datepicker using pickadate.js

I am attempting to configure the options for the angular-datepicker, which is utilizing pickadate, by fetching data from a web-api call. Here is my current code: <label for="datepicker">Date:</label> <input type="text" id="datepicker" pick ...

How can I properly integrate md5 hash in an Angular 2 typescript file?

Struggling with implementing md5 hash for user passwords in Angular 2. Despite importing the Md5 module in my typescript file, it doesn't seem to be recognized when I run the application. https://i.sstatic.net/dxtd2.png Even though Visual Studio reco ...

Is it possible to incorporate a selection box along with the Raycaster in Three.js?

In my GLTF scene, I have been exploring the use of the example selection box (code) to select multiple meshes. Unfortunately, the current approach is providing inaccurate results as it selects based on the centroid of each mesh and includes meshes that ar ...

If the variable is empty, use jQuery ajax to send a request with different data

Below is the code I have written: $(document).ready(function() { cookieanimalid =($.cookie("cookieanimal")); $.ajax({ type: 'POST', url: "/myurl/", data: {cookieanimalid}, success: function ( ...

Guidance on editing list items individually using jQuery from separate input fields

Working with li presents some challenges for me. On my webpage, I have an input field that dynamically adds values to a list. However, the function to edit these values is not working properly. After editing an li element, it deletes all classes and span ...

Trouble fetching asynchronous data in Next.js Higher Order Component

Currently, I am in the process of creating a Higher Order Component (HOC) that will fetch user data from my API and then provide props to the wrapped component. This will allow the component to redirect the user based on their role. Although the HOC appea ...

Guide to changing a 10-digit number field to a string field in MongoDB

Looking to change the mobile field to a string in MongoDB. { "_id": "1373b7723", "firstname": "name1", "mobile":1000000099 }, { "_id": "137be30723", "firstname": "name2&qu ...

How exactly does the indexOf() method function when used with an array as the search value?

For some reason, when I input single letter words like "s" or "z" into the array it functions properly. However, in this particular case, it's not working and I can't figure out why. It seems to be related to the condition, as when I just use "do ...

Enhancing WordPress Menu Items with the 'data-hover' Attribute

Looking for a solution to add "data-hover" to menu items on Wordpress, like: Wanting to insert: data-hover="ABOUT US" into <a href="#">ABOUT US</a> without manually editing the link's HTML to make it: <a href="#" data-hover="ABOU ...

Received the error message "Material-UI: capitalize(string) expects a string argument" while implementing the snackbar feature in a React Material-UI project

While working with Material-UI, I came across an issue with the snackbar where I received an error message saying: Error: Material-UI: capitalize(string) expects a string argument. Here's a snippet of my code: this.state = { snackBarOpenVer ...

The file upload feature is not functioning properly within a repeater inside an update panel

In order to prevent automatic postback, I implemented an update panel on my ASP webpage that contains several repeaters. Each repeater includes a file upload control. However, when attempting to save, the file upload is displaying an empty file. ...

C# programming with Selenium WebDriver for Headless Browsing and Unit Testing: A Comprehensive Introduction

After several months of using Selenium Webdriver in C# and gaining proficiency, I have encountered a scenario at work where I need to test the UI of a web-based browser product with multiple users simultaneously. As I have previous experience with jMeter f ...

The React Component is limited to updating once

Exploring the React Native code below: import React from 'react'; import {Text, View, StyleSheet, Button} from 'react-native'; export default class Target extends React.Component { constructor(props){ super(props); ...

Using Selenium to interact with drop-down lists using div tags instead of select tags

As a newcomer to automated testing using Selenium Web Driver, I am struggling to test drop down lists for the location type without relying on the select command. The element in question is enclosed within a div tag. I attempted sending keys but that meth ...

How can checkboxes be combined from two separate tables?

Within this interactive code example, you will find two tables containing checkboxes in each row. Upon clicking the To button, a dialog box will appear allowing you to select users or groups from a drop-down menu. The corresponding tables will then be dis ...