Try this:
var images = {
0: 'images/pic1.png',
1: 'images/pic2.png',
2: 'images/pic3.png'
}
function setImage(elementId, imageIndex) {
document.getElementById(elementId).src = images[imageIndex];
}
You can create the array server side using code similar to this:
string[] files = System.IO.Directory.GetFiles("d:\\picture");
System.Text.StringBuilder sb = new StringBuilder();
string relativePath = "images";
int fileIndex = 0;
sb.AppendLine("var images = {");
foreach (string file in files)
{
System.IO.FileInfo fi = new FileInfo(file);
sb.AppendLine((fileIndex > 0 ? "," : "") + fileIndex + ": '" + relativePath + "/" + fi.Name + "'");
}
sb.AppendLine("};");
System.Web.UI.HtmlControls.HtmlGenericControl script = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
script.Attributes.Add("language", "javascript");
script.Attributes.Add("type", "text/javascript");
script.InnerText = sb.ToString();
this.Controls.Add(script);