title: Adding server click to dynamic asp button in aspnet gridview header
body: <p>I have to add a button in the gridview header spanning two columns.  I do this in the code behind: </p>
  
  <pre><code>        if (e.Row.RowType == DataControlRowType.Header)
          {
              GridView HeaderGrid = (GridView)sender;
              GridViewRow HeaderGridRow =
              new GridViewRow(0, 0, DataControlRowType.Header,
              DataControlRowState.Insert);  
              TableCell HeaderCell = new TableCell(); 
              HeaderCell.Text = "Name of Person";
              HeaderCell.Font.Bold = true;
              HeaderCell.ColumnSpan = 1;
              HeaderGridRow.Cells.Add(HeaderCell);
              HeaderCell = new TableCell();
  
              Button btnHeader = new Button();
              btnHeader.UseSubmitBehavior = false;
              btnHeader.Click += new EventHandler(this.btnsubmitentry_Click);
              btnHeader.Text = "Add New Task";
              btnHeader.CommandName = "AddNewTask";
              //  OnClick="btnsubmitentry_Click"
              btnHeader.CssClass = "btn btn-lg btn-default";
              btnHeader.OnClientClick = "AddNewTask();";
              HeaderCell.Controls.Add(btnHeader);
              HeaderCell.ColumnSpan = 2;
              HeaderGridRow.Cells.Add(HeaderCell);
  
              grdViewSummary.Controls[0].Controls.AddAt(0, HeaderGridRow);
          }
  </code></pre>
  
  <p>This generates a nice button, however I need help how to invoke the server side button click event.</p>
  
  <pre><code>&lt;input type="button" name="grdViewSummary$ctl01$ctl00" value="Add New Task" onclick="AddNewTask();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&amp;quot;grdViewSummary$ctl01$ctl00&amp;quot;, &amp;quot;&amp;quot;, true, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;, false, true))" class="btn btn-lg btn-default"   /&gt;
  </code></pre>
  
  <p>As you can see, I passed in a onClientClick to a javascript function that has the postback code, but the event is not fired.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function AddNewTask() {
          alert();
          __doPostBack('btnsubmitentry_Click','')
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>I also have tried passing in the eventhandler and CommandName. But none of these are shown in the view source of the button in question. 
  I will be okay with any approach; calling postback on javascript function or, calling the server side click routine directly or, invokes the commandname grdiview function.</p>