title: Call a Function on Dynamically Generated ASP:Button in ASP.NET c#
body: <p>I'm trying to call a function created in code through <code>&lt;asp:Button&gt;</code>'s on click event but its not working... </p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {       
              SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
              ArrayList myArrayList = ConvertDataSetToArrayList();
              Literal objliteral = new Literal();
              StringBuilder objSBuilder = new StringBuilder();
  
          // Display each item of ArrayList
          foreach (Object row in myArrayList)
          {
              objSBuilder.Append("&lt;div class='AppItem'&gt;");                
              objSBuilder.Append("&lt;label class='control-label span3'&gt;" + ((DataRow)row)["PlanName"].ToString() + "&lt;/label&gt;");
              objSBuilder.Append("&lt;label class='control-label span3'&gt; Plan Description :" + ((DataRow)row)["PlanDesc"].ToString() + "&lt;/label&gt;");
              objSBuilder.Append("&lt;label class='control-label span3'&gt; Price :" + ((DataRow)row)["PlanCost"].ToString() + "&lt;/label&gt;");
              objSBuilder.Append("&lt;label class='control-label span3'&gt; Total Visitors :" + ((DataRow)row)["PlanVisitors"].ToString() + "&lt;/label&gt;");
              objSBuilder.Append("&lt;div class='control-group'&gt;&lt;asp:Button Text='AddtoCart' runat='server' id='" + ((DataRow)row)["PlanId"].ToString() + "' class='btn btn-small btn-success' onclick='AddPlanToCart();' /&gt;&lt;/div&gt;");
  
              //&lt;asp:Button ID="ImageButton1" runat="server" Text="Upload" CssClass="btn btn-small btn-success" onclick="ImageButton1_Click" /&gt;
              objSBuilder.Append("&lt;/div&gt;");
          }
          objliteral.Text = objSBuilder.ToString();
          PlanContent.Controls.Add(objliteral);
  }
  
  private void AddPlanToCart()
  {    
          //This does not get called.
  }
  </code></pre>
  
  <p>Click <a href="https://www.dropbox.com/s/tmtf86l5nr4z00l/How%20to%20call%20a%20function%20on%20dynamically%20generated%20ASPButton%20in%20asp.net%20c%23.txt" rel="nofollow" title="How to call a function on dynamically generated AS...in asp.net c#">here</a> to see code behind!</p>