title: Setting ASP.NET Button attributes client side and read attribute value server side
body: <p>How can I retrieve a <code>Button</code> custom attribute after the attribute value has been changed using javascript?</p>
  
  <p>Example:</p>
  
  <p><strong>Asp file</strong></p>
  
  <pre><code>&lt;asp:Button ID="Button1" runat="server" Text="Button1" /&gt;
  &lt;asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" /&gt;
  
  &lt;script type="text/javascript"&gt;
  var btn1 = '#&lt;% Button1.ClientID %&gt;';
  var btn2 = '#&lt;% Button2.ClientID %&gt;';
  
  $(btn1).click(function(e) {
      e.preventDefault();
      $(btn2).attr("actIndex", "2");
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>CodeBehind file</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostBack)
          Button2.Attributes.Add("actIndex","1");
  }
  
  protected void Button2_Click(object sender, EventArgs e)
  {
       Button btn = (Button)sender;
  
       // this should be 2 if button1 has been clicked
       string actIndex = btn.Attributes["actIndex"]; 
  }
  </code></pre>
  
  <p>If I click <code>Button1</code> then I click <code>Button2</code> the <code>actIndex</code> value is still "1" but if I use page inspect the <code>Button2</code> <code>actIndex</code> attribute is "2", somehow the attribute value is not passed to postBack action.</p>
  
  <p>How can I solve this mystery?</p>