ImageButton.Click event never gets fired!
-
Hi guys! I'm creating a web application with a table containing files the users have uploaded to the site. Now for every entry in the table I have a little "trash" icon, so when clicked, the system will delete the file. Now - here is my problem: It doesn't work! Surprise huh! ;) Well it seems like the Command event of the ImageButton never gets fired. I've tried having it throw an exception, but nothing happens, the page just reposts whenever the image is clicked. I do not use the ASP.NET data table WebControl (the one you see in almost every tutorial about ASP.NET and data access) - because it does not fit into my needs. And to be honest: I don't like it that much either. I have this code in my ASP.NET page's CodeBehind file's Page_Load method (please don't think to much about the variable names, as some of them are in Danish):
// This code sample is part of a foreach loop, so try to imagine
// that the imgb object gets added to the current table row at the end of every loop.
// That's also why I set the .ID to "cmd" + the id for the file in the database.
// Result:
// The ImageButton turns up on the page, you can click the button, the page reposts
// and then: nada happens.ImageButton imgb = new ImageButton(); imgb.CommandArgument = b.DbId.ToString(); imgb.CommandName = "Delete";
// Attach to the Command event - or what?
// Tried with new CommandEventHandler(this.DeleteFile) - doesn't work either.
imgb.Command += this.DeleteFile;imgb.ImageUrl = "~/Images/trash.gif"; imgb.ID = "cmd" + b.DbId.ToString();
I have this code in the "DeleteFile" method, which matches the Delegate for the Command event:
protected void DeleteFile(object sender, CommandEventArgs e) { Int32 bilagId = Int32.Parse(e.CommandArgument.ToString()); BilagHandler.Delete(bilagId); throw new Exception("wtf!"); FillBilagTable(); }
The WTF Exception never gets throwed - ergo: The code never executes! Any clues? I'm really confused. And perhaps this post is a stupid question, but I'm just code-blind by now - hey - so be it! :-) BTW: The code compiles without any errors. Best regards Soeren
-
Hi guys! I'm creating a web application with a table containing files the users have uploaded to the site. Now for every entry in the table I have a little "trash" icon, so when clicked, the system will delete the file. Now - here is my problem: It doesn't work! Surprise huh! ;) Well it seems like the Command event of the ImageButton never gets fired. I've tried having it throw an exception, but nothing happens, the page just reposts whenever the image is clicked. I do not use the ASP.NET data table WebControl (the one you see in almost every tutorial about ASP.NET and data access) - because it does not fit into my needs. And to be honest: I don't like it that much either. I have this code in my ASP.NET page's CodeBehind file's Page_Load method (please don't think to much about the variable names, as some of them are in Danish):
// This code sample is part of a foreach loop, so try to imagine
// that the imgb object gets added to the current table row at the end of every loop.
// That's also why I set the .ID to "cmd" + the id for the file in the database.
// Result:
// The ImageButton turns up on the page, you can click the button, the page reposts
// and then: nada happens.ImageButton imgb = new ImageButton(); imgb.CommandArgument = b.DbId.ToString(); imgb.CommandName = "Delete";
// Attach to the Command event - or what?
// Tried with new CommandEventHandler(this.DeleteFile) - doesn't work either.
imgb.Command += this.DeleteFile;imgb.ImageUrl = "~/Images/trash.gif"; imgb.ID = "cmd" + b.DbId.ToString();
I have this code in the "DeleteFile" method, which matches the Delegate for the Command event:
protected void DeleteFile(object sender, CommandEventArgs e) { Int32 bilagId = Int32.Parse(e.CommandArgument.ToString()); BilagHandler.Delete(bilagId); throw new Exception("wtf!"); FillBilagTable(); }
The WTF Exception never gets throwed - ergo: The code never executes! Any clues? I'm really confused. And perhaps this post is a stupid question, but I'm just code-blind by now - hey - so be it! :-) BTW: The code compiles without any errors. Best regards Soeren
Wire up the handler to the Click event, not Command.
-
Wire up the handler to the Click event, not Command.
It does not work either. Neither the Click or the Command event gets fired. I forgot to write, that I've tried the Click event as well. Btw. the both the command event and the click event should be fired when the ImageButton is clicked. The Command Event gives me the benefit of having access to the CommandArgument, which can tell me the id of the file to be deleted. The click event cannot. Best Regards Soeren
-
It does not work either. Neither the Click or the Command event gets fired. I forgot to write, that I've tried the Click event as well. Btw. the both the command event and the click event should be fired when the ImageButton is clicked. The Command Event gives me the benefit of having access to the CommandArgument, which can tell me the id of the file to be deleted. The click event cannot. Best Regards Soeren
Try this:
imgb.Command += new CommandEventHandler(this.DeleteFile);
EDIT ------------- nm, saw you tried it with new as well... Does it even do a post back? Try setting the AutoPostBack property to true;
-
Try this:
imgb.Command += new CommandEventHandler(this.DeleteFile);
EDIT ------------- nm, saw you tried it with new as well... Does it even do a post back? Try setting the AutoPostBack property to true;
It does produce a postback. I've even tried setting the PostBackUrl property, even though it should not be necessary. My theory is that I forgot to set some property that makes the ImageButton fire its events, but I can't find out which property it is. It makes no sense. :(( Best Regards Soeren