AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1864&pId=-1
Asp.net mvc image gallery
page
by Mohammed Salah
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 31717/ 45

Introduction

      In this tutorial Mohammed will show how to use the jquery image gallery "Galleria" which is wrapped as HtmlHelper , as well as illustrating how to make HtmlHelpers

Required Files

    Galleria Image gallery is a jquery plugin that works with jquery version 1.26 , So we need to add some css and javascript references in our project as following :

     jquery-1.2.6.js : jquery base javascript

     jquery.galleria.js : The Image Gallery Plugin "Galleria"

    JimageGallery.js : it has the jquery code the calls functions from jquery.galleria.js

    galleria.css : this style sheet file is used by jquery script to apply css style on images

   GalleryStyle.css : this css is defined by user to make the thumb images in vertical Orientation 

   HGalleryStyle.css : this css is defined by user to make the thumb images in Horizontal             Orientation

How it Works

    First I will illustrate how to use Galleria plugin with in simple html example

 

<script type="text/javascript" src=" jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery.galleria.js"></script>
<link href="galleria.css" rel="stylesheet" type="text/css" media="screen">
<link href=" HGalleryStyle.css.css" rel="stylesheet" type="text/css" 
      media="screen">
// to make thumb images in horizontal orientation
jQuery(function($) {
            
            $('.gallery_demo_unstyled').addClass('gallery_demo'); 
            // adds new class name to maintain degradability
            
            $('ul.gallery_demo').galleria({
                  history   : true, 
// activates the history object for bookmarking, back-button etc.
                  clickNext : true, // helper for making the image clickable
                  insert    : '#main_image',
// the containing selector for our main image
                  onImage   : function(image,caption,thumb) { 
// let's add some image effects for demonstration purposes
                        
                        // fade in the image & caption
                        if(! ($.browser.mozilla && 
                        navigator.appVersion.indexOf("Win")!=-1) ) { 
// FF/Win fades large images terribly slow
                              image.css('display','none').fadeIn(1000);
                        }
                        caption.css('display','none').fadeIn(1000);
                        
                        // fetch the thumbnail container
                        var _li = thumb.parents('li');
                        
                        // fade out inactive thumbnail
                        _li.siblings().children('img.selected').fadeTo(500,0.3);
                        
                        // fade in active thumbnail
                        thumb.fadeTo('fast',1).addClass('selected');
                        
                        // add a title for the clickable image
                        image.attr('title','Next image >>');
                  },
                  onThumb : function(thumb) { // thumbnail effects goes here
                        
                        // fetch the thumbnail container
                        var _li = thumb.parents('li');
                        
                        // if thumbnail is active, fade all the way.
                        var _fadeTo = _li.is('.active') ? '1' : '0.3';
                        
                        // fade in the thumbnail when finnished loading
                        thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
                        
                        // hover effects
                        thumb.hover(
                              function() { thumb.fadeTo('fast',1); },
                              function() { 
_li.not('.active').children('img').fadeTo('fast',0.3); } 
// don't fade out if the parent is active
                        )
                  }
            });
      });
// I have put the previous code in  JimageGallery.js
 
<div class="demo">
<div id="main_image"></div>
<ul class="gallery_demo_unstyled">
 //…..here we will put list of images 
    <li><img src="/imagepath" alt="Sea Mist" title="Caption text from title"></li>
</ul>
<p class="nav"><a href="#" onclick="$.galleria.prev(); return false;">&laquo; 
previous</a> | <a href="#" onclick="$.galleria.next(); return false;">next 
&raquo;</a></p>
</div>

For more information go to http://code.google.com/p/galleria/

How to make Basic HtmlHelper Method

   The Goal of creating custom htmlhelper is to reduce the html content you write if you use the template many times as you can get data and format it in html form  , So Html Helper method acts as wrapper for data and html as it returns string of html .

    Creating HtmlHelper is simply using Extension Method , Like this

      public static class MyCustomHtmlHelper
    {
        public static string CustomHtmlHelper(this HtmlHelper html, 
string htmlContent)
        {
           return "<p>this my first html helper </p>";
        }
     }
Take a look inside JphotoGalleryExt HtmlHelper Method
public static string  JPhotoGalleryExt(this  HtmlHelper html, 
List<string> photoList, int imageHeight, int imageWidth,
 Orientation orientation)
        {
            StringBuilder References = new StringBuilder();
 
            StringBuilder ImageList = new StringBuilder();
            StringBuilder MainImageStyle = new StringBuilder();
            StringBuilder HorizontalGallery = new StringBuilder();
            TagBuilder galleryStyle = new TagBuilder("style");
        
            References.Append( html.JavaScript("jquery-1.2.6.js"));
            References.Append(   html.JavaScript("jquery.galleria.js"));
            galleryStyle.Attributes["type"= "text/css";
            MainImageStyle.Append(" #main_image {   ");
            string Height = "height: " + imageHeight.ToString() + "px;";
            string Width = "widht: " + imageHeight.ToString() + "px;";
 
            References.Append ( html.CSSFile("galleria.css"));
         if (orientation == Orientation.Vertical)
 
     References.Append(html.CSSFile("GalleryStyle.css"));
         else if (orientation == Orientation.Horizontal)
         {
             References.Append(html.CSSFile("HGalleryStyle.css"));
             HorizontalGallery.Append(galleryStyle.ToString());
         }
               References.Append (html.JavaScript("JimageGallery.js"));
              galleryStyle.Attributes["media"] = "screen,projection";
 
          
            MainImageStyle.Append(Height);
            MainImageStyle.Append(Width);
            MainImageStyle.Append("}");
            galleryStyle.InnerHtml = MainImageStyle.ToString();
         
            // Here is the html of Horizontal gallery style 
          
            HorizontalGallery.Append(References.ToString());
          // 
     HorizontalGallery.Append( "<div class='demo'>");
     HorizontalGallery.Append(" <div id='main_image'></div>");
     HorizontalGallery.Append("<ul class='gallery_demo_unstyled'>");
     foreach (string photo in photoList)
     {
         HorizontalGallery.Append(("<li><img src='"+photo+"' alt='Stones'
            title='Stones - from Apple images'></li>"));
     }
        HorizontalGallery.Append("</ul>");
     HorizontalGallery.Append("<p class='nav'><a href='#'
 onclick='$.galleria.prev(); return false;'>&laquo; previous</a> |
 <a href='#' onclick='$.galleria.next(); return false;'>next &raquo;</a></p>");
 
     HorizontalGallery.Append("</div>");
             
           return  HorizontalGallery.ToString(); 
        }

 

First we create a method that takes a generic collection of photo Paths and the size of Main Image (Height and Width), then add reference files listed in Required Files , we uses Orientation enum to make the developer chooses his Orientation.  

 

How to use the HtmlHelper

You will use that HtmlHelper like any helper. Just call a function and give it image list, height, width, and orientation.

<%=Html.JPhotoGalleryExt(new List<String>(){"img1.jpg","img2.jpg",   
       100,300, global::Orientation.Vertical) %>

Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 10:57:28 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search