I found some code on the net which is used to preload
images for ImageButtons which are contained in a User
Control. I'm trying to adapt this code to use in my aspx.cs
files:
private void Page_PreRender(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
System.Web.UI.WebControls.Image DirectionMap = new
System.Web.UI.WebControls.Image();
System.Web.UI.WebControls.Image DirectionStore = new
System.Web.UI.WebControls.Image();
DirectionMap.ImageUrl = "WebImages/MainSite/Map.jpg";
DirectionStore.ImageUrl = "WebImages/MainSite/Mandrake_Bldg_2.jpg";
this.Controls.Add(DirectionMap);
this.Controls.Add(DirectionStore);
if (!Page.IsStartupScriptRegistered(this.ID + "preloadImages"))
{
string sURL;
System.Web.UI.WebControls.Image im = null;
StringBuilder sbFront = new StringBuilder();
StringBuilder sbBack = new StringBuilder();
int iCount = 0;
foreach (Control c in this.Controls)
{
if (c is System.Web.UI.WebControls.Image)
{
im = (System.Web.UI.WebControls.Image)c;
sURL = im.ImageUrl;
sbBack.Append(@"preloadImages[" +
iCount.ToString() +
"] = new Image();");
sbBack.Append(@"preloadImages[" +
iCount.ToString() +
"].src = '" +
sURL +
"'; \n");
iCount++;
}
}
if (iCount > 0)
{
sbBack.Append(@"</script>");
string sCount = iCount.ToString();
sbFront.Append(@"<script language=""JavaScript"">");
sbFront.Append(@"var preloadImages = new Array(" +
sCount +
"); \n");
Page.RegisterStartupScript(this.ID +
"preloadImages", sbFront.ToString() +
sbBack.ToString());
}
}
else
{
Response.Write(this.ID + ".Page_PreRender: Already registered " + this.ID +
"_preload_images");
}
}
I run this code and it preloads my images just great.
However, the images don't appear in the image controls.
They appear in newly created and mysterious image
tags at the bottom of the page, outside the bottom
HTML tag. Very interesting! It seems that that the
image that is used in interating over the control
collection persists and holds the preloaded image.
Huh?