|
-
Aug 18th, 2006, 03:40 PM
#1
Thread Starter
Hyperactive Member
[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
-
Aug 19th, 2006, 04:10 PM
#2
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.
-
Aug 20th, 2006, 08:44 AM
#3
Thread Starter
Hyperactive Member
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.
-
Aug 20th, 2006, 09:03 AM
#4
Thread Starter
Hyperactive Member
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);
}
-
Aug 21st, 2006, 08:55 AM
#5
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.
-
Aug 21st, 2006, 12:09 PM
#6
Thread Starter
Hyperactive Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|