title: How to pass a C# variable as a CommandArgument in ASP.NET
body: <p>I am creating dynamically an array, adding a line for each entry of some data. Each entry must have a button to do some stuff with it, but I am stuck at this point. I have this code to create the <em>ASP.NET</em> button: </p>
  
  <pre><code>&lt;asp:Button ID="buttonyes" class="buttonyes" CommandArgument='&lt;%= request.ID %&gt;' 
              CommandName="AcceptRequest" OnClick='&lt;%= ButtonPressed %&gt;' runat="server"/&gt;
  </code></pre>
  
  <p>But when I run it, I have multiple errors saying that:</p>
  
  <blockquote>
    <p>The <code>%=</code> and <code>&lt;</code> or <code>&gt;</code> expressions are not valid.</p>
  </blockquote>
  
  <p>When I click on the button, I want to call a function written in the <strong>index.aspx.cs</strong> file linked with my <strong>index.aspx</strong>, here is <code>ButtonPressed</code> function and with a filter in this function, it choose what action it should do:</p>
  
  <pre><code>protected void ButtonPressed(Object sender, CommandEventArgs e)
  {
      Button btn = (Button)sender;
      switch (btn.CommandName)
      {
          case "AcceptRequest":
              AcceptRequest(btn.CommandArgument.ToString());
              break;
          case "RefuseRequest":
              RefuseRequest(btn.CommandArgument.ToString());
              break;
      }
  }
  </code></pre>
  
  <p>The problem is that I don't succeed in passing those <em>C#</em> variables as a <code>CommandArgument</code> and <code>CommandName</code>.</p>
  
  <p>I already tried those syntaxes, following some previous questions:</p>
  
  <ul>
  <li><code>CommandArgument="&lt;%: request.ID %&gt;"</code></li>
  <li><code>CommandArgument='&lt;%: request.ID %&gt;'</code></li>
  <li><code>CommandArgument="&lt;%= request.ID %&gt;"</code></li>
  <li><code>CommandArgument='&lt;%= request.ID %&gt;'</code></li>
  </ul>
  
  <p>But none of them worked.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>As <a href="https://stackoverflow.com/users/5265292/grek40">@grek40</a> said below, the <code>OnClick</code> value should be replace like the following: </p>
  
  <pre><code>&lt;asp:Button ID="buttonyes" class="buttonyes" CommandArgument='&lt;%= request.ID %&gt;' 
              CommandName="AcceptRequest" OnCommand="ButtonPressed" runat="server"/&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong> </p>
  
  <p>I would like to know how to pass a C# <strong><em>local</em></strong> variable since those button should be added in a <code>foreach</code> loop like so:</p>
  
  <pre><code>foreach (Request request in requests)
       {
       %&gt;
           &lt;tr&gt;
                &lt;td&gt;&lt;%: request.applicant.Name + " (" + request.applicant.ID + ")" %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%: request.software.Name %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%: request.date %&gt;&lt;/td&gt;
                &lt;td class="actions"&gt;
                     &lt;asp:Button ID="buttonyes" class="buttonyes" CommandName="AcceptRequest" CommandArgument="&lt;# request.Id %&gt;"  OnCommand="ButtonPressed" runat="server"/&gt;
                     &lt;asp:Button ID="buttonno" class="buttonno" CommandName="RefuseRequest" CommandArgument="&lt;# request.Id %&gt;" OnCommand="ButtonPressed" runat="server"/&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;%
        }
  </code></pre>
  
  <p>By the way, the code above doesn't work, since, as <a href="https://stackoverflow.com/users/6170890/nino">@Nino</a> said, this only works for global variables.</p>