Click Event for ImageButtons created in code and persisted from Session.
I am building a web site for a friend who is a graphic designer.
He wants to display one image at a time on his home page and have this image selected by clicking on one of a related group of thumbnails.
I have an <asp:Panel id="ThumbnailList"... > hard coded in the page markup and I'm creating ImageButton instances within my C# code behind from the rows in a DataTable.
Getting the group of ImageButton's to appear on the page is working, but when I click on them I can't get any life out of my ThumbnailClick(object sender, ImageClickEventArgs e) event handler.
The code goes a little something like this...
private const string THUMBNAIL_KEYS = "THUMBNAIL_KEYS";
private List<string> sessionThumbKeys;
var queryInfoArgs = "...";
DataTable dt = FetchDataTable(queryInfoArgs)
foreach(DataRow r in dt.Rows)
string fileID = r["fileID"];
string imageName = r["name"];
string imageURL = r["url"];
ImageButton imageButton = CreateNewThumbnail(fileID,imageName,imageURL);
string sessionKey = "ImageButton_" + fileID;
protected ImageButton CreateNewThumbnail(string fileID, string name, string uRL)
ImageButton imageButton = new ImageButton();
imageButton.ID = "ImageButton_" + fileID;
imageButton.ToolTip = name;
imageButton.ImageUrl = uRL;
// Information about the file selected to be sent back when clicked
imageButton.Click += new ImageClickEventHandler(ThumbnailClick);
// Click wasn't working so I added the following
protected void ThumbnailClick(object sender, ImageClickEventArgs e)
.... retrieve fileID, query dataBase for url and set up the full version of the image on the page}
Because the ImageButtons are not in the markup and don't get added when the Page constructor runs I have to bring them back out of session. Since it's all a bit new to me, I'm not 100% sure I'm doing this at the right point in the page lifecycle.
protected override void OnPreLoad(EventArgs e)
if (Session[THUMBNAIL_KEYS] != null)
foreach (string s in (List<string>)Session[THUMBNAIL_KEYS])
if (Session[s] != null)
ImageButton i = (ImageButton)Session[s];
This loads them back into the page nicely but the event doesn't fire.
I initially only set the Click property on the ImageButtons, then I thought about the fact that the ImageButtons get placed into session before they are rendered, so when pulling them out of Session again it's as if they had never been clicked.
So I then added the OnClientClick string, I can get the Request.Form["__EVENTTARGET"] and Request.Form["__EVENTARGUMENT"] information back nicely at page load and display it on a couple of labels.
I've started to look at Page methods such as RaisePostBackEvent() and RegisterRequiresRaiseEvent()
e.g. during the OnPreLoad foreach...
foreach (string s in Session[THUMBNAIL_KEYS...
ImageButton i = Session[s] as ImageButton;
if(i.UniqueID == Request.Form["__EVENTTARGET"])
but no joy so far.
I'd really appreciate some pointers!
Can anyone help? If you can I'll run straight out on Tuesday morning and buy you a cut price Easter Egg!
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)