title: Create buttons dynamically from code behind in ASP.NET
body: <p>I'm quite new to ASP.NET and I need your help.</p>
  
  <p>I'm programming on an application which should help to fix frequent issues. Users can click the displayed cases if it describes their problem. The application searches for more cases or displays a possible solution.</p>
  
  <p>Now what I need for this is some code which creates the buttons dynamically. I googled some ideas and created some code, however I was not able to get it to work.</p>
  
  <p>It works to create the first selection of buttons with the <code>Default_Load</code> method. Also the <code>OnClick</code> event (<code>ButtonClick_System</code>) works fine which means I get the next selection.
  From here it starts messing around. The dynamic buttons created in <code>ButtonClick_System</code> don't have a working <code>OnClick</code> action. </p>
  
  <p>Instead of proceeding with <code>ButtonClick_Question</code> (because of <code>btn_system.Command += ButtonClick_Question;</code> in <code>ButtonClick_System</code>) it seems like it just loads the homepage (maybe something wrong with <code>Page_Load</code>?).</p>
  
  <p>The application should do <code>ButtonClick_Question</code> until no more datasets available in database.</p>
  
  <p>I got the following code:</p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Data;
  using Oracle.DataAccess.Client;
  
  namespace Application
  {
      public partial class _default : System.Web.UI.Page
      {
          // Variables
          private string app_name = "Application";
  
          // ----- Page_Load ----- //
          protected void Page_Load(object sender, EventArgs e)
          {
                  Default_Load();
                  Session["Application"] = app_name;
          }
  
          // ----- Methods ----- //
          // Load homepage
          public void Default_Load()
          {
              pnl_default.Visible = true;
              pnl_content.Visible = false;
              HtmlGenericControl html_default = new HtmlGenericControl();
              html_default.TagName = "div";
  
              string cmdString = "(...)";
              DataTable dtSystems = OraQueryData(cmdString);
              foreach (DataRow dtRow in dtSystems.Rows)
              {
                  int system_id = Convert.ToInt32(dtRow["SYSTEM_ID"]);
                  string system_name = Convert.ToString(dtRow["SYSTEM_NAME"]);
                  var btn_system = new Button
                  {
                      ID = "btn_" + system_name,
                      Text = system_name,
                      CssClass = "sys_buttons"
                  };
                  btn_system.Command += ButtonClick_System;
                  btn_system.CommandArgument = Convert.ToString(system_id);
                  html_default.Controls.Add(btn_system);
              }
              plh_default.Controls.Clear();
              plh_default.Controls.Add(html_default);
          }
  
          // Button OnClick Events
          protected void ButtonClick_System(object sender, CommandEventArgs e)
          {
              pnl_default.Visible = false;
              pnl_content.Visible = true;
              HtmlGenericControl html_questions = new HtmlGenericControl();
              html_questions.TagName = "div";
  
              int system_id = Convert.ToInt32(e.CommandArgument);
              string cmdString = "(...)";
              DataTable dtQuestions = OraQueryData(cmdString);
  
              foreach (DataRow dtRow in dtQuestions.Rows)
              {
                  string question_id = Convert.ToString(dtRow["FRAGE_ID"]);
                  string question_text = Convert.ToString(dtRow["FRAGE_TEXT"]);
                  var btn_system = new Button
                      {
                          ID = "btn_question" + question_id,
                          Text = question_text,
                          CssClass = "quest_buttons"
                      };
              btn_system.Command += ButtonClick_Question;
              btn_system.CommandArgument = Convert.ToString(system_id);
              html_questions.Controls.Add(btn_system);
              }
              plh_content.Controls.Clear();
              plh_content.Controls.Add(html_questions);
          }
  
          protected void ButtonClick_Question(object sender, CommandEventArgs e)
          {
              pnl_default.Visible = false;
              pnl_content.Visible = true;
              HtmlGenericControl html_ChildQuestions = new HtmlGenericControl();
              html_ChildQuestions.TagName = "div";
  
              int parent_id = Convert.ToInt32(e.CommandArgument);
              string cmdString = "(...)";
              DataTable dtChildQuestions = OraQueryData(cmdString);
  
              foreach (DataRow dtRow in dtChildQuestions.Rows)
              {
                  string question_id = Convert.ToString(dtRow["FRAGE_ID"]);
                  string question_text = Convert.ToString(dtRow["FRAGE_TEXT"]);
                  var btn_system = new Button
                  {
                      ID = "btn_question" + question_id,
                      Text = question_text,
                      CssClass = "quest_buttons"
                  };
                  btn_system.Command += ButtonClick_Question;
                  btn_system.CommandArgument = question_id;
                  html_ChildQuestions.Controls.Add(btn_system);
              }
              plh_content.Controls.Clear();
              plh_content.Controls.Add(html_ChildQuestions);
          }
  
          // ----- Oracle Data Query Methods ----- //
          // Create and execute query on database
          public static DataTable OraQueryData(string cmdString)
          {
              string conString = ConfigurationManager.AppSettings["Connection"];
              OracleConnection oraCon = new OracleConnection(conString);
              OracleCommand oraCmd = new OracleCommand(cmdString, oraCon);
              OracleDataAdapter oraDtAd = new OracleDataAdapter(oraCmd.CommandText, oraCon);
              DataTable dt = new DataTable();
              oraCon.Open();
              oraDtAd.Fill(dt);
              oraCon.Close();
              return dt;
          }
      }
  }
  </code></pre>