Dynamic image button
Hello
I am having a unique problem with a webpage I am working on. I have searched high and low for an answer but was unable to find one.
I currently have a dynamically generated image button that does 2 things. When the page first loads, it is set to grab the Image thumbnail related to that particular product via ProductID, when the User clicks on the imagebutton, a popup window comes up with the large original image.
I have found a way to make that work, the problem I am running into is that everytime an image button is clicked, the whole page reloads. This can be a real problem for people who use the back button as well as a slow internet connection. :(
Here are some code of what I did: (Since I don't know how to generate a button dynamically at run-time, I have preset the buttons with the visibility at false, so when I call the functions during run time, only the buttons with an image and a URL will be visible. If anyone can help me with a better way around that, that would be awesome!) :D
public partial class Products : System.Web.UI.Page
{
string strConn = ConfigurationSettings.AppSettings["CMDB"];
int intProductID = 0;
private void Page_Load(object sender, System.EventArgs e)
{
DisplayImages();
}
protected void DisplayImages()
{
string strImg = String.Empty;
string popupImg = String.Empty;
string strImg2 = String.Empty;
string popupImg2 = String.Empty;
//Image imgMain = new Image();
ImageButton imgBtn = new ImageButton();
for (int i = 1; i < 25; i++)
{
strImg = "ProductThumbnail/" + intProductID + "_" + i + "Thumb.jpg";
popupImg = "ProductImages/" + intProductID + "_" + i + ".jpg";
strImg2 = "ProductThumbnail/" + intProductID + "_" + i + "Thumb.gif";
popupImg2 = "ProductImages/" + intProductID + "_" + i + ".jpg";
imgBtn = (ImageButton)Page.FindControl("ImageButton" + i);
if (File.Exists(MapPath(strImg)))
{
imgBtn.ImageUrl = strImg;
imgBtn.Visible = true;
imgBtn.Attributes.Add("onClick", "javascript:popImage('" + popupImg + "')");
}
else if (File.Exists(MapPath(strImg2)))
{
imgBtn.ImageUrl = strImg2;
imgBtn.Attributes.Add("onClick", "javascript:popImage('" + popupImg2 + "')");
imgBtn.Visible = true;
}
else
imgBtn.Visible = false;
}
}
|