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.
// Information about the file selected to be sent back when clicked
imageButton.Attributes.Add("file_id",fileID);
// Events
imageButton.Click += new ImageClickEventHandler(ThumbnailClick);
// Click wasn't working so I added the following
// Borrowing the javascript doPostBack function used by HiddenFields
imageButton.OnClientClick = String.Format("javascript:__doPostBack('ct100$ContentPlaceHolderPageBody${0}','{1}')",imageButton.ID ,fileID);
.... 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.
foreach (string s in (List<string>)Session[THUMBNAIL_KEYS])
{
if (Session[s] != null)
{
ImageButton i = (ImageButton)Session[s];
ThumbnailList.Controls.Add(i);
}
}
}
base.OnInit(e);
}
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()
Bookmarks