title: ASP.NET (C#) - click event for dynamically generated ImageButtons
body: <p>in my website I'm generating seven ImageButtons from which only one (random) is enabled. I want to generate for that button a click event that is triggered only when a specific key combination is pressed (e.g: when pressing E+Click).</p>
  
  <p>Thanks for the help.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          //Generates a &lt;int, string&gt; dictionary
          LoginHelper.CreateDictionary(images);
  
          //'buttons' is an int list
          while (buttons.Count &lt; 7)
          {
              //generates a random number from 1 to 7
              int number = LoginHelper.GenerateNumber();
  
              if (buttons.Contains(number) == false)
              {
                  buttons.Add(number);
                  ImageButton btn = new ImageButton();
                  btn.CssClass = "loginButtons";
                  btn.ImageUrl = (from x in images
                                  where x.Key == number
                                  select x.Value).First();
                  //gets the link string according to the randomized number
                  btn.PostBackUrl = LoginHelper.GetLink(number);
  
                  if (btn.PostBackUrl == string.Empty)
                  {
                      btn.Enabled = false;
                  }
  
                  btn.Click += new ImageClickEventHandler(btn_Click);
  
                  footer.Controls.Add(btn);
              }
          }
      }
      //The event is not triggered
      void btn_Click(object sender, ImageClickEventArgs e)
      {
          ImageButton button = sender as ImageButton;
          ConsoleKeyInfo cki = Console.ReadKey();
  
          if (cki.Key == ConsoleKey.E)
          {
              button.PostBackUrl = "~/about.aspx";
          }
      }
  </code></pre>