ImageButton event doesn't fire but Button event does

Discussion in 'ASP .Net' started by suzanne.boyle@gmail.com, Jun 13, 2007.

  1. 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.
     
    , Jun 13, 2007
    #1
    1. Advertising

  2. On Jun 13, 1:30 am, wrote:
    > 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);
    }
     
    Alexey Smirnov, Jun 13, 2007
    #2
    1. Advertising

  3. 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.
     
    , Jun 13, 2007
    #3
  4. On Jun 13, 7:33 pm, wrote:
    > 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
     
    Alexey Smirnov, Jun 13, 2007
    #4
  5. Guest

    On 13 Jun, 21:18, Alexey Smirnov <> wrote:
    > On Jun 13, 7:33 pm, wrote:
    >
    > > 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


    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.
     
    , Jun 13, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Janet Collins
    Replies:
    0
    Views:
    1,162
    Janet Collins
    Jan 13, 2006
  2. Nathan Sokalski
    Replies:
    0
    Views:
    1,098
    Nathan Sokalski
    Jan 15, 2009
  3. MU
    Replies:
    0
    Views:
    575
  4. Andy B.
    Replies:
    0
    Views:
    906
    Andy B.
    Nov 25, 2009
  5. KJ
    Replies:
    2
    Views:
    204
    Stevie_mac
    Aug 16, 2003
Loading...

Share This Page