title: Pass C# Variable to HTML Button
body: <p>I'm trying to dynamically change the text/value of 5 HTML buttons on a web page, via passing C# variables to those buttons. I'm generating the variables via a SQL query in the page load but can't figure out how to pass the variables to the buttons.</p>
  
  <p>Variable generation:</p>
  
  <pre><code>        DataSet ds= new DataSet();
          DataTable dt= new DataTable();
          connection.Open();
          string commandstring = "SELECT TOP (5) [ButtonVal] FROM Table";
          SqlDataAdapter adptr = new SqlDataAdapter(commandstring, connection);
          adptr.Fill(ds);
          dt = ds.Tables[0];
          Btn1 = System.Convert.ToString(dt.Rows[0][0]);
          Btn2 = System.Convert.ToString(dt.Rows[1][0]);
          Btn3 = System.Convert.ToString(dt.Rows[2][0]);
          Btn4 = System.Convert.ToString(dt.Rows[3][0]);
          Btn5 = System.Convert.ToString(dt.Rows[4][0]);
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>    &lt;table&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;asp:Button ID="Button1" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt; 
   &lt;td&gt;&lt;asp:Button ID="Button2" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;        
   &lt;td&gt;&lt;asp:Button ID="Button3" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;     
   &lt;td&gt;&lt;asp:Button ID="Button4" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;   
   &lt;td&gt;&lt;asp:Button ID="Button5" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;
  &lt;tr /&gt;
  </code></pre>
  
  <p></p>
  
  <p>The OnClick function is redirecting to another paged based on the button's value. </p>
  
  <p><strong>* EDIT BASED ON Jim W's ANSWER *</strong></p>
  
  <pre><code>        1)
            C#:
              public string Btn1
              if (!Page.IsPostBack)
              {
                 Btn1 = (dt.Rows[0][0]).ToString();
              }
  
            HTML:
              &lt;td&gt;&lt;asp:Button ID="Button1" Text="&lt;%# Btn1 %&gt;" Value ="&lt;%# Btn1 %&gt;" 
              style="font-size:8px;height:30px;width:60px" runat="server"  
              AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;
  
            Output:
               Blank Button 
  
          2)
            C#:
  
              if (!Page.IsPostBack)
              {
                 Button1.Text = (dt.Rows[0][0]).ToString();
              }
  
            HTML:
              &lt;td&gt;&lt;asp:Button ID="Button1" Text="&lt;%# Button1 %&gt;" Value ="&lt;%# Button1 %&gt;" 
              style="font-size:8px;height:30px;width:60px" runat="server"  
              AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;
  
            Output:
               Button text is "System.Web.UI.WebControls.Button" 
  
          3)
            C#:
              public string Btn1
              if (!Page.IsPostBack)
              {
                 Btn1 = System.Convert.ToString(dt.Rows[0][0]);
              }
  
            HTML:
              &lt;td&gt;&lt;asp:Button ID="Button1" Text="&lt;%# Btn1 %&gt;" Value ="&lt;%# Btn1 %&gt;" 
              style="font-size:8px;height:30px;width:60px" runat="server"  
              AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;
  
            Output:
               Blank Button             
  
          4)
            C#:
              public string Btn1
              if (!Page.IsPostBack)
              {
                 Btn1 = (dt.Rows[0][0]).ToString();
              }
  
            HTML:
              &lt;td&gt;&lt;asp:Button ID="Button1" Text="&lt;%# Btn1 %&gt;" Value ="&lt;%# Btn1 %&gt;" 
              style="font-size:8px;height:30px;width:60px" runat="server"  
              AutoPostBack="true"  OnClick="ChangeRedirect_Click" /&gt;   &lt;/td&gt;
  
            Output:
               Blank Button 
  </code></pre>