title: How to create an onClick eventHandler for a dynamically created button
body: <p>Currently, I am doing a project for students' hostel and now I have to implement some search strategies about students.Here I have to create a button dynamically when the user clicks on the another server button in .aspx page and accordingly I have to create the onclick event handler for the newly created button. The code-snippet that I used is:</p>
  
  <pre><code>protected void btnsearchByName_Click(object sender, EventArgs e)
      {
          TextBox tbsearchByName = new TextBox();
          Button btnsearchName = new Button();
          tbsearchByName.Width = 250;
          tbsearchByName.ID = "tbsearchByName";
          tbsearchByName.Text = "Enter the full name of a student";
          btnsearchName.ID = "btnsearchName";
          btnsearchName.Text = "Search";
          btnsearchName.Click += new EventHandler(this.btnsearchName_Click);
  
          pnlsearchStudents.Controls.Add(tbsearchByName);
          pnlsearchStudents.Controls.Add(btnsearchName);
      }
       protected void btnsearchName_Click(object sender, EventArgs e)
      {
          lblsearch.Text = "btnsearchName_Click event fired in " + DateTime.Now.ToString();
  
      }
  </code></pre>
  
  <p>Here, the problem is newly created eventHandler doesnot get fired. I have gone through this site and looked several questions and answers and also gone through the page life-cycle and they all say that the dynamic button should be on Init or Pre_init, but my problem is I have to create it when another button is clicked, how can it be possible?</p>