vcbertini
asked on
Need good image preloader solution
We use the Sitecore content management system with C# ASP.NET controls.
I am working on a directory project and have uploaded the employee photos into the Sitecore Media Library into a folder called images/Faculty. There are about 250 faculty photos loaded and 727 faculty total, so this means that about 400+ faculty have not had their photo taken yet, so an “image not found” photo shows in place of their headshot.
The way I have programmed this is, in the code-behind the image name is automatically created based on the faculty member’s name as such:
Name: Dave Smith
Black & White photo name: SmithDave1.jpg
Color photo name: SmithDave2.jpg
Now, if the application loads and a faculty photo exists in Sitecore, then all is well with the world and it loads. If no faculty photo exists, the default “image not found” photo appears. Well, the following code *is* working, but it is SUPER slow. Here is my code:
I was wondering if there is a better way to program a sub-function to see if a URL exists before actually returning an error. Here is what I *want* to do, but it is not working
Can you help?? Do you have any brilliant ideas?
I am working on a directory project and have uploaded the employee photos into the Sitecore Media Library into a folder called images/Faculty. There are about 250 faculty photos loaded and 727 faculty total, so this means that about 400+ faculty have not had their photo taken yet, so an “image not found” photo shows in place of their headshot.
The way I have programmed this is, in the code-behind the image name is automatically created based on the faculty member’s name as such:
Name: Dave Smith
Black & White photo name: SmithDave1.jpg
Color photo name: SmithDave2.jpg
Now, if the application loads and a faculty photo exists in Sitecore, then all is well with the world and it loads. If no faculty photo exists, the default “image not found” photo appears. Well, the following code *is* working, but it is SUPER slow. Here is my code:
string imageLink = relative + "/Faculty/" + fileName + "1.ashx";
string imageHover = relative + "/Faculty/" + fileName + "2.ashx";
string imageDefault = relative + "/Faculty/ImageNotAvailable1.ashx";
ImageButton myImage = ((ImageButton)e.Item.FindControl("ibHeadshot"));
myImage.ImageUrl = imageLink;
myImage.Attributes.Add("onmouseover", "this.src='" + imageHover + "'");
myImage.Attributes.Add("onmouseout", "this.src='" + imageLink + "'");
myImage.Attributes.Add("onerror", "this.src='" + imageDefault + "'");
I was wondering if there is a better way to program a sub-function to see if a URL exists before actually returning an error. Here is what I *want* to do, but it is not working
Boolean validURL = true;
string imageLink = relative + "/Faculty/" + fileName + "1.ashx";
string imageHover = relative + "/Faculty/" + fileName + "2.ashx";
string imageDefault = relative + "/Faculty/ImageNotAvailable1.ashx";
//check to see if a headshot exists - if it does not, use the default image
validURL = UrlExists(imageLink);
ImageButton myImage = ((ImageButton)e.Item.FindControl("ibHeadshot"));
if (validURL)
{
myImage.ImageUrl = imageLink;
myImage.Attributes.Add("onmouseover", "this.src='" + imageHover + "'");
myImage.Attributes.Add("onmouseout", "this.src='" + imageLink + "'");
}
else
{
myImage.ImageUrl = imageDefault;
}
private static bool UrlExists(string path)
{
Sitecore.Pipelines.HttpRequest.ItemResolver
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem(path);
if ((item != null))
{
return true;
}
else
{
return false;
}
}
Can you help?? Do you have any brilliant ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window