ASPX templates
Version 5.x > ASPX templates > MediaGallery Selected Item View modes: 
User avatar
Member
Member
DahlinDev - 4/21/2010 3:25:55 PM
   
MediaGallery Selected Item
I am using the MediaGallery control. Is there a way on the Selected Item to have a Prev and Next link which will then simply navigate to the previous or next photo in the Media Library?

I'd like to do this instead of having to go back to the thumbnails.

User avatar
Member
Member
DahlinDev - 4/22/2010 8:43:25 AM
   
RE:MediaGallery Selected Item
I ended up creating a couple of custom macros to achieve this.
So in my MediaGallerySelectedItem transformation I have:

<a href="<%# MediaLibraryFunctions.GetMediaFileNext(UrlHelper.CurrentURL, Eval("FileID")) %>" class="next">Next</a>

<a href="<%# MediaLibraryFunctions.GetMediaFilePrevious(UrlHelper.CurrentURL, Eval("FileID")) %>" class="next">Previous</a>


These functions are then defined in App_Code > CMSModules > Media Library > MediaLibraryFunctions.cs

    public static string GetMediaFileNext(string currentUrl, object fileId)
{
string albumUrl = UrlHelper.RemoveParameterFromUrl(currentUrl, "fileid");

return MediaLibrary.GetNextUrl(albumUrl, Convert.ToInt32(fileId));
}


MediaLibrary namespace is in a class library I built outside of the Kentico sln.

Here is the code for that class, you need to make sure to reference the dll and include the using statement for it. Alternatively you should be able to put this right in the App_Code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NatureValley.Common
{
public static class MediaLibrary
{
/// <summary>
/// Gets the url to the next File in that album
/// </summary>
/// <param name="albumUrl"></param>
/// <param name="fileId"></param>
/// <returns></returns>
public static string GetNextUrl(string albumUrl, int fileId)
{
string nextUrl = albumUrl + "&fileid=";
int[] mediaFiles;
using (NVdatabaseDataContext db = new NVdatabaseDataContext(Globals.ConnStr))
{
var mediaFile = db.Media_Files.Single(mf => mf.FileID == fileId);
string filePath = mediaFile.FilePath.Substring(0, mediaFile.FilePath.Length - mediaFile.FileName.Length+1);

mediaFiles = (from mf in db.Media_Files
where mf.FilePath.StartsWith(filePath)
orderby mf.FileID
select mf.FileID).ToArray();
}

if (mediaFiles != null && mediaFiles.Length > 1)
{
for (int i = 0; i < mediaFiles.Length; i++)
{
if (mediaFiles == fileId)
{
if (i != mediaFiles.Length - 1)
{
nextUrl = nextUrl + mediaFiles[i + 1].ToString();
}
else
{
nextUrl = nextUrl + mediaFiles[0].ToString();
}
}
}
}
else
{
nextUrl = nextUrl + fileId.ToString();
}

return nextUrl;
}

/// <summary>
/// Gets the url to the previous File in that album
/// </summary>
/// <param name="albumUrl"></param>
/// <param name="fileId"></param>
/// <returns></returns>
public static string GetPreviousUrl(string albumUrl, int fileId)
{
string nextUrl = albumUrl + "&fileid=";
int[] mediaFiles;
using (NVdatabaseDataContext db = new NVdatabaseDataContext(Globals.ConnStr))
{
var mediaFile = db.Media_Files.Single(mf => mf.FileID == fileId);
string filePath = mediaFile.FilePath.Substring(0,mediaFile.FilePath.Length - mediaFile.FileName.Length+1);

mediaFiles = (from mf in db.Media_Files
where mf.FilePath.StartsWith(filePath)
orderby mf.FileID
select mf.FileID).ToArray();
}

if (mediaFiles != null && mediaFiles.Length > 1)
{
for (int i = 0; i < mediaFiles.Length; i++)
{
if (mediaFiles == fileId)
{
if (i != 0)
{
nextUrl = nextUrl + mediaFiles[i - 1].ToString();
}
else
{
nextUrl = nextUrl + mediaFiles[mediaFiles.Length - 1].ToString();
}
}
}
}
else
{
nextUrl = nextUrl + fileId.ToString();
}

return nextUrl;
}
}
}



Notice I am using linq to sql here so you'll have to set that dbml up too. I pointed to the table Media_File in the database.

This ended up working well, but seemed to be a lot to get what I needed. Is there a simpler solution out there?

User avatar
Member
Member
Snarrak - 11/18/2011 9:01:04 AM
   
RE:MediaGallery Selected Item
Hi DahlinDev,

I also have a dbml file in my App_code, this works when running from Visual Studio. But when I export my site, the DataContext isn't recognized.
So i'm getting a compilation error on the following code:


var db = new DataClassesDataContext();
var homeImages = from m in db.Media_Files
where m.FilePath.Contains("Home/") && m.FileLibraryID == 8
select m;


Where did you put your dbml file?

Regards,
Jorik