Sorry for the quick and dirty explanation in the beginning, let me elaborate some more. I have a user control (gallery.ascx) with a datalist in it, so it looks something like this:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank">
<asp:Image ID="Image1" runat="server"
imageurl='<%# "~/Thumbnail.ashx?image=" + OceanitData.ToURLEncoding("~\\Files"
+ DataBinder.Eval(Container.DataItem, "file_path")) + "&height=206" %>'
OnLoad='<%# "preloadImage (~\\Files" + Eval("file_path") + ")" %>' />
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>
Basically, there's a folder with a bunch of images in it. The DataList gets bound to the SQL table that has the paths to all the files in that particular folder. The imageurl
for the <asp:Image>
is set to run the custom handler Thumbnail.ashx
, which generates a thumbnail and returns the response to <asp:Image>
so that the client gets a thumbnail instead of the full image. However, what I want to do is also have the original image download in the background so when the user clicks on the thumbnail, the large image is already downloaded to the cache and displays instantly.
The problem I have is that I don't know the path to the files until the DataList is bound to the SQL table, so I have to run the Javascript function inside the DataList. However, based on the code I just provided above, this is the HTML output I get (simplified):
<a href="pic.jpg" target="_blank"><img OnLoad="preloadImage(pic.jpg)"
src="Thumbnail.ashx?image=pic.jpg&height=206" /></a>
Internet Explorer tells me "Error on page." so it's not running preloadImage()
. I hope this helps.
Oh, by the way, I changed the Javascript function to this:
<script type="text/javascript">
function preloadImage(path)
{
loadImage = new Image();
loadImage.src = path;
}
</script>
This function is put into the main page (not my user control), under the <head runat="server">
tag.