title: ASP.NET add onclick to div via code behind
body: <p>I'm using ASP.NET and I want to be able to click on a <code>&lt;div&gt;</code> and set the background to a color (prefably an image later on). This <code>&lt;div&gt;</code> is generated by code:</p>
  
  <pre><code>public void generate (int[] _blocks, int _width, int _height, int _distance) {
          int HorizontalBlocks = _blocks[0];
          int VerticalBlocks = _blocks[1];
          int top = 0;
          int left = 0;
  
          for (int i = 0; i &lt; VerticalBlocks; i++) {
              for (int x = 0; x &lt; HorizontalBlocks; x++) {
                  System.Web.UI.HtmlControls.HtmlGenericControl Div = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
  
                  Div.ID = "Box_" + x + "_" + i;
                  Div.Style.Add(HtmlTextWriterStyle.Width, _width.ToString() + "px");
                  Div.Style.Add(HtmlTextWriterStyle.Height, _height.ToString() + "px");
                  Div.Style.Add(HtmlTextWriterStyle.Top, top.ToString() + "px");
                  Div.Style.Add(HtmlTextWriterStyle.Left, left.ToString() + "px");
                  Div.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#848484");
                  Div.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                  Div.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                  Div.Attributes.Add("runat", "server");
                  Div.Attributes.Add("onclick", "setBackground();");
  
                  grid.Controls.Add(Div);
  
                  left += _width + _distance;
              }
  
              left = 0;
              top += _height + _distance;
          }
      }
  </code></pre>
  
  <p>I have tried creating the method <code>setBackground();</code> but I can't seem to figure out how to set the background of the currently clicked <code>&lt;div&gt;</code>.</p>