title: adding event handler to a dynamically created button
body: <p>I have created a button in c# codebehind and am trying to add event listener to it, but the problem is if I use:</p>
  
  <pre><code>    {
          HtmlGenericControl dodaj = new HtmlGenericControl("button");
          dodaj.InnerText = "something";
          dodaj.Attributes.Add("runat", "server");
          dodaj.Attributes.Add("type", "submit");
          dodaj.Attributes.Add("onclick","addKosarica");
          newDivInfo.Controls.Add(dodaj);
          sadrzaj.Controls.Add(newDivInfo);
          this.Controls.Add(sadrzaj);
      }
  
      protected void addKosarica(object sender, EventArgs e)
      {
          Response.Redirect("www.google.com");  //just to see if it triggers
      }
  </code></pre>
  
  <p>I get: </p>
  
  <blockquote>
    <p>"Uncaught ReferenceError: addKosarica is not defined at
    HTMLButtonElement.onclick"</p>
  </blockquote>
  
  <p>Tried googling the error, it is regarding javascript...</p>
  
  <p>Then after googling some more, I tried:</p>
  
  <pre><code>    {
          Button dodaj = new Button;
          dodaj.Text = "something";
          dodaj.Click += new EventHandler("addKosarica");
          newDivInfo.Controls.Add(dodaj);
          sadrzaj.Controls.Add(newDivInfo);
          this.Controls.Add(sadrzaj);
      }
  
      protected void addKosarica(object sender, EventArgs e)
      {
          Response.Redirect("www.google.com");//just to see if it triggers
      }
  </code></pre>
  
  <p>and i get</p>
  
  <blockquote>
    <p>"Control 'ctl07' of type 'Button' must be placed inside a form tag
    with runat=server."</p>
  </blockquote>
  
  <p>This is my aspx code:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="11001.aspx.cs" Inherits="_11001" %&gt;
  
  &lt;asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>(its not much because i add everything dynamically and everything works except button onclick handler...)</p>
  
  <p>Please, could someone tell me how to make this working...</p>
  
  <p>EDIT: 
  sadrzaj is a div that contains all this elemnts I'm adding.
  I'm adding them in a function that is being called from Page_Load()</p>