Answered by:
ImageButton won't fire click event (Composite Control).

Question
-
User-400304391 posted
Hi!
I'm creating a Composite Control which holds an asp:ImageButton, but it wont fire the click event i've added to it. I've tried adding an ordinary asp:Button and it works perfectly.
The control is instantiated in CreateChildControls like this:
imageBtn = new ImageButton();
imageBtn.ImageUrl = "../Images/Button.JPG";
imageBtn.AlternateText = "Send";
imageBtn.Click += new ImageClickEventHandler(imageBtn_Click);And the click event is defined like this:
public void imageBtn_Click(object sender, ImageClickEventArgs e)
{
int i = 0;
}The button is then rendered in the Render method like this:
imageBtn.RenderControl(writer);
I don't know what I'm missing since an ordinary button works as it should and it's defined almost exactly as the ImageButton, except for using EventHandler instead of
ImageClickEventHandler, any ideas?
Regards/sZ
Friday, June 20, 2008 4:04 AM
Answers
-
User-667042492 posted
hi,
where do you add this event in your custom class? You can add event like that..
protected override void OnLoad(EventArgs e){
base.OnLoad(e);
imageBtn.Click +=
new ImageClickEventHandler(imageBtn_Click);}
and the other way below, maybe solve your problem;
public
static readonly object ImageClick = new object();public
event ImageClickEventHandler ImageClicked
{
add
{
this.Events.AddHandler(ImageClick, value);
}
remove
{
this.Events.RemoveHandler(ImageClick, value);
}
} protected virtual void OnImageClick(ImageClickEventArgs args) // i'm not sure for the method name{
Delegate handler = this.Events[ImageClick]; if (null != handler) handler.DynamicInvoke(this, args);}
i hope this help you..
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 20, 2008 1:03 PM -
User-1407570774 posted
Hi there,
1 using System.Web.UI; 2 using System.Web.UI.WebControls; 3 4 namespace WebApplication1 5 { 6 7 [ToolboxData("<{0}:ImgBtn runat=\"server\" />")] 8 public class ImgBtn : CompositeControl, INamingContainer 9 { 10 11 protected override void CreateChildControls() 12 { 13 ImageButton ctl = new ImageButton(); 14 ctl.ImageUrl = "~/access_16x16.gif"; 15 ctl.Click += new ImageClickEventHandler(ctl_Click); 16 17 this.Controls.Add(ctl); 18 19 base.CreateChildControls(); 20 } 21 22 private void ctl_Click(object sender, ImageClickEventArgs e) 23 { 24 throw new System.NotImplementedException(); 25 } 26 27 } 28 29 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 21, 2008 5:35 AM
All replies
-
User1968518401 posted
Hi r u able to view image?
Friday, June 20, 2008 9:34 AM -
User1174340047 posted
I don't have a idea why the event handler is called, but i think you don't have to write code to render the image buton because it will render by itself.
Friday, June 20, 2008 10:56 AM -
User-667042492 posted
hi,
where do you add this event in your custom class? You can add event like that..
protected override void OnLoad(EventArgs e){
base.OnLoad(e);
imageBtn.Click +=
new ImageClickEventHandler(imageBtn_Click);}
and the other way below, maybe solve your problem;
public
static readonly object ImageClick = new object();public
event ImageClickEventHandler ImageClicked
{
add
{
this.Events.AddHandler(ImageClick, value);
}
remove
{
this.Events.RemoveHandler(ImageClick, value);
}
} protected virtual void OnImageClick(ImageClickEventArgs args) // i'm not sure for the method name{
Delegate handler = this.Events[ImageClick]; if (null != handler) handler.DynamicInvoke(this, args);}
i hope this help you..
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 20, 2008 1:03 PM -
User-1407570774 posted
Hi there,
1 using System.Web.UI; 2 using System.Web.UI.WebControls; 3 4 namespace WebApplication1 5 { 6 7 [ToolboxData("<{0}:ImgBtn runat=\"server\" />")] 8 public class ImgBtn : CompositeControl, INamingContainer 9 { 10 11 protected override void CreateChildControls() 12 { 13 ImageButton ctl = new ImageButton(); 14 ctl.ImageUrl = "~/access_16x16.gif"; 15 ctl.Click += new ImageClickEventHandler(ctl_Click); 16 17 this.Controls.Add(ctl); 18 19 base.CreateChildControls(); 20 } 21 22 private void ctl_Click(object sender, ImageClickEventArgs e) 23 { 24 throw new System.NotImplementedException(); 25 } 26 27 } 28 29 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 21, 2008 5:35 AM -
User-400304391 posted
Hello and thanks for the replies!
I set the click event in createchildcontrols. I haven't had the chance to try your suggestions yet, will do it first thing tomorrow. Really appreciated.
Regards
/sZ
Sunday, June 29, 2008 10:56 AM -
User1044508394 posted
Hi Shakazed,
Can I please ask how you get your ImageBotton to fire click event in your composite control? I know it's so long ago, but I'm hoping you could help because I'm having the same issue.
Thanks heaps
-cb-
Tuesday, August 25, 2009 6:04 PM