[2005] Event handler never called.
The code below works, except the lButton_Click event handler is never called when any of the ImageButtons are clicked. I've done this loads of times in 2003, what am I doing wrong in 2005?
ImageMaker.aspx is just a WebForm that BinaryWrites the applicable Image as a byte[].
Code:
protected void Page_Load(object sender, EventArgs e)
{
this.getThumbnails();
}
private void getThumbnails()
{
System.Web.UI.WebControls.ImageButton lButton;
foreach (string lFile in Directory.GetFiles("C:\\Graphics\\Ethan"))
{
lButton = new System.Web.UI.WebControls.ImageButton();
lButton.ID = lFile;
lButton.ImageUrl = "ImageMaker.aspx?ImageAddress=" + lButton.ID;
lButton.Click +=new ImageClickEventHandler(this.lButton_Click);
this.pnlThumbnails.Controls.Add(lButton);
}
}
private void lButton_Click(object sender, ImageClickEventArgs e)
{
ImageButton lButton = sender as ImageButton;
this.imgMain.ImageUrl = "ImageMaker.aspx?ImageAddress=" + lButton.ID + "&ImageType=full";
th
Re: [2005] Event handler never called.
You forgot the Page.IsPostBack check. :)
I'm hesitant to explain this to you, as you might know... but anyways: Everytime you click on the button, the page load event is called first. And you are assigning the click event in the page load event, as a result, the event that was originally meant to be called, isn't called, due to the reset.
Re: [2005] Event handler never called.
No, that's not it, this.getThumbnails must be called every time to create and populate the ImageButtons. The delegate for the event handler will, or should, ensure it is called no matter if new ImageButtons are created.
Just to make sure I added an IsPostBack check and didn't create the new Imagebuttons, this had no effect, the handler was still not called.
Re: [2005] Event handler never called.
This does work, what is wrong with the first one?
Code:
protected void Page_Load(object sender, System.EventArgs e)
{
this.addButton();
}
private void addButton()
{
ImageButton lButton = new ImageButton();
lButton.ID = "Click";
lButton.Click += new ImageClickEventHandler(lButton_Click);
this.pnlMain.Controls.Add(lButton);
}
void lButton_Click(object sender, ImageClickEventArgs e)
{
ImageButton lButton = sender as ImageButton;
Response.Write(lButton.ID);
}
Re: [2005] Event handler never called.
I cannot tell why it works, it's weird behavior. But the delegate cannot ensure that it is called everytime, as it can lose viewstate information in re-assignment. It's a common scenario, and yet I'm not sure why yours works.
The only difference I see is that you aren't creating any new images in your latest code, just creating one new image button.
Re: [2005] Event handler never called.
I tried assigning a .jpg using ImageMaker.apsx, in the second example and it worked again.
First one still doen't work!