I'm not sure if I'm even going to be able to explain my problem, but here goes:
I have a user control that is supposed to display an image, and a list of LinkButtons that cause the displayed image to change.
As each LinkButton is clicked, it fires an event handler that causes for the list to be reloaded, as well as the image to be changed to the one requested.
The onclick events are assigned to the list of LinkButtons at run-time with an AddHandler call.
I have a function called LoadInfo that is called on Page_Load. LoadInfo pulls the list of images out of the database, and writes the list of LinkButtons. It assigns the event handlers to each LinkButton in a loop, and loads the image. LoadInfo is also called from the LinkButton_onClick event handler, so that whenever a LinkButton is clicked, it causes the list of images to be reloaded, as well as the current image.
The problem is:
The first time the page loads, it calls loadInfo. Works great. I can then click on one of the LinkButtons in the list, and it reloads the control with the new image, and the link in the list is highlighted to show which image I selected. However, when I try to click on a different link AFTER the first PostBack, it just reloads the page. It doesn't call my event handler, it only reloads the page. This leads me to suspect that my event handlers were not assigned to the LinkButtons. After the loadInfo function has been called from the Page_Load again, it all works. So, I am essentially required to click on an image title TWICE to get it to load the correct image.
The other problem I had was how .NET would lose the contents of a variable between page loads. On the OnClick event of one of the image links I would assign the id of the link clicked to a global variable. However, that value would be lost, and the list would reset itself the next time I clicked a link and reloaded the control. I solved that by assigning the id to the viewstate. That maintains it regardless of what's going on on the page. However, I still have the problem of clicking twice to get a response.
It all seems to come down to the fact that after the postback, the page resets itself, variables and all. And on the postback, it doesn't properly assign event handlers if they are being assigned dynamically. It's like the page needs to start with a clean slate after the postback for it to allow another postback.
I don't know if any of this makes sense, but I've been banging my head off of it for two days and I'm ready to move on to something else.
Please help! Thanks!