title: Add OnClick to button from codebehind
body: <p>I am trying to add an OnClick attribute to a button from the code behind. Depending if the Attending element is 0 or not will determine which OnClick attribute gets added. When I click the button with the code below I get the following error:</p>
  
  <blockquote>
    <p>"Microsoft JScript runtime error:
    Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback
    or callback argument.  Event validation is enabled using  in configuration or &lt;%@ Page
    EnableEventValidation="true" %> in a page.  For security purposes,
    this feature verifies that arguments to postback or callback events
    originate from the server control that originally rendered them.  If
    the data is valid and expected, use the
    ClientScriptManager.RegisterForEventValidation method in order to
    register the postback or callback data for validation."</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>ASPX</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" EnableEventValidation="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test.CommunityEvents.Default" %&gt;
   &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:DataList ID="DataList1" RepeatColumns="1" CellPadding="5" OnItemDataBound="Dl1_ItemDataBound"
              runat="server"&gt;
              &lt;ItemTemplate&gt;
                     &lt;div id="Attendingbox" runat="server"&gt;
                         &lt;asp:Label ID="AttendingorNot" runat="server"&gt;&lt;/asp:Label&gt;
                      &lt;/div&gt;
                      &lt;br /&gt;
                      &lt;asp:Button ID="SignupButton" runat="server" Text="" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:DataList&gt;
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>Code Behind</p>
  
  <pre><code>protected void Dl1_ItemDataBound(object sender, DataListItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {
              .....//removed other code to save space
  
              Button SignupButton = (Button)e.Item.FindControl("SignupButton");
              if (Attending == 0)
              {
                  AttendingorNot.Text = "You are not attending";
                  AttendingorNot.Attributes.Add("class", "alert");
                  SignupButton.Text = "Attend";
                  SignupButton.Attributes.Add("class", "btn btn-large btn-success");
                  SignupButton.Click += new EventHandler(Submit_Add);
              }
              else
              {
                  AttendingorNot.Text = "You are attending!";
                  AttendingorNot.Attributes.Add("class", "alert alert-success");
                  SignupButton.Text = "Remove";
                  SignupButton.Attributes.Add("class", "btn btn-large btn-danger");
                  SignupButton.Click += new EventHandler(Submit_Remove);
              }
          }
      }
      private void Submit_Remove(object sender, EventArgs e)
      {
          Response.Redirect("Default.aspx?msg=work");
      }
      private void Submit_Add(object sender, EventArgs e)
      {
          Response.Redirect("Default.aspx?msg=gone");
      }
  </code></pre>