ImageButton event doesn't fire but Button event does


S

suzanne.boyle

Hi,
I'm adding an ImageButton as a child to a custom web control and
attaching an event handler to it. When the page posts back the event
is not being fired however. I initially assumed I had missed some
code or something however after playing about I've found that if I
change the control to a Button (or a LinkButton) the event fires.

Here is the code called in the overridden CreateChildControls method.

ImageButton btn = new ImageButton();
btn.ID = "test";
btn.AlternateText = "test";
btn.Command += new CommandEventHandler(btn_Command);

Like I say, if I change the ImageButton to a Button (and obviously
change AlternateText to Text) the event fires ok.

Can anyone shed some light on why Button works but ImageButton doesn't.
 
Ad

Advertisements

G

Guest

Hi,
I'm adding an ImageButton as a child to a custom web control and
attaching an event handler to it. When the page posts back the event
is not being fired however. I initially assumed I had missed some
code or something however after playing about I've found that if I
change the control to a Button (or a LinkButton) the event fires.

Here is the code called in the overridden CreateChildControls method.

ImageButton btn = new ImageButton();
btn.ID = "test";
btn.AlternateText = "test";
btn.Command += new CommandEventHandler(btn_Command);

Like I say, if I change the ImageButton to a Button (and obviously
change AlternateText to Text) the event fires ok.

Can anyone shed some light on why Button works but ImageButton doesn't.

This works for me

protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.ID = "test";
btn.AlternateText = "test";
btn.CommandName = "test";
btn.CommandArgument = "1";
btn.Command += new CommandEventHandler(btn_Command);
form1.Controls.Add(btn);
}

void btn_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandName + e.CommandArgument);
}
 
S

suzanne.boyle

I've found what is causing the problem. I have some javascript on the
page that hides ImageButtons container html element when it is
clicked. If I remove the javascript that does this it works.
 
G

Guest

I've found what is causing the problem. I have some javascript on the
page that hides ImageButtons container html element when it is
clicked. If I remove the javascript that does this it works.

Great! Glad you got it working
 
Ad

Advertisements

S

suzanne.boyle

Great! Glad you got it working

I never actually got the ImageButton working. It seems when I capture
the document click event or the click event of the ImageButton's
container in javascript it stops the image button postback working
correctly. Instead I fudged a solution using a link button with an
image control inside.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top