Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ImageButton.Click event never gets fired!

ImageButton.Click event never gets fired!

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-netdatabasehelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MY1201
    wrote on last edited by
    #1

    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

    E 1 Reply Last reply
    0
    • M MY1201

      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

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      Wire up the handler to the Click event, not Command.

      M 1 Reply Last reply
      0
      • E eggsovereasy

        Wire up the handler to the Click event, not Command.

        M Offline
        M Offline
        MY1201
        wrote on last edited by
        #3

        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

        E 1 Reply Last reply
        0
        • M MY1201

          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

          E Offline
          E Offline
          eggsovereasy
          wrote on last edited by
          #4

          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;

          M 1 Reply Last reply
          0
          • E eggsovereasy

            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;

            M Offline
            M Offline
            MY1201
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups