title: ASP.NET: How to avoid situation that dynamically created button event refreshes the page instead of doing relevant code
body: <p>(<strong>Original text updated!</strong>)</p>
  <p>I am slowly getting into ASP.NET, currently trying to make a small webforms app for a hackathon.</p>
  <p>The page that I am having problems with is supposed to show a list of companies to which person can apply.</p>
  <p>Since the list of those companies can change, company always can be added or removed, I want to make some kind of table which loads values dynamically. The table appears when a user clicks &quot;View all companies&quot;, it shows a table with each row including the name of company and a button to move to the page of this company. At this stage, I am trying to make something even more simple, for the sake of testing.</p>
  <p>So, the button should just display some kind of text.</p>
  <p>But here's the problem.
  When I click on the button, it just resets the page and as I understand, runs &quot;Page_Load&quot;. The text is also resets to the value from Page_Load.</p>
  <p>Now, I'll try to give code and try to give the best descriptions.</p>
  <p>Here's the Page_Load, on first load it just displays the name of the user, and hides the table with company names. On non-first load, it should run click button events.</p>
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {            
          HRGlobalHub.Code.Start.Firebase.thisFirebaseClient = new FireSharp.FirebaseClient(HRGlobalHub.Code.Start.Firebase.thisFirebaseConfig);
  
          pnlCompaniesView.Visible = false;
  
          if (!IsPostBack)
          {
              if (thisUser == null)
                  lblWelcomeUser.Text = &quot;Welcome&quot;;
              else
                  lblWelcomeUser.Text = &quot;Welcome, &quot; + thisUser.FirstName + &quot; &quot; + thisUser.LastName;
  
              gCompany = new Code.Company.Company();
          }
  
          else
          {
              if (Action==&quot;Apply&quot;)
                  btnApplyClick(this, e);
              else if (Action == &quot;Suggest&quot;)
                  btnSuggestClick(this, e);
              if (Action == &quot;View&quot;)
                  btnViewClick(this, e);
          }
      }
  </code></pre>
  <p>The code for showing all companies comes here:</p>
  <pre><code>    protected async void btnViewAllCompanies_Click(object sender, EventArgs e)
      {
          ResetCompaniesTable();
  
          lblWelcomeUser.Text = &quot;ViewAll&quot;;
  
          FirebaseResponse response = await HRGlobalHub.Code.Start.Firebase.thisFirebaseClient.GetTaskAsync(&quot;HRGlobalHub/Database/Companies/CompaniesList/&quot;);
  
          HRGlobalHub.Code.Company.CompaniesNamesList thisData = response.ResultAs&lt;HRGlobalHub.Code.Company.CompaniesNamesList&gt;();
          string[] CompanyArray = thisData.CompanyNameListVar.Split(',');
  
          for (int i = 1; i &lt; CompanyArray.Length; i++)
          {               
              response = await HRGlobalHub.Code.Start.Firebase.thisFirebaseClient.GetTaskAsync(&quot;HRGlobalHub/Database/Companies/CompaniesDatabase/&quot; + CompanyArray[i]);
              HRGlobalHub.Code.Company.Company result = response.ResultAs&lt;HRGlobalHub.Code.Company.Company&gt;();
  
              tbtTestTable.Rows.Add(NewCompanyRow(result));                
          }
  
          pnlCompaniesView.Visible = true;
          pnlCompaniesView.Width = 1100;
          tbtTestTable.Width = 1100;
          lblWelcomeUser.Text = tbtTestTable.Visible.ToString();
      }
  </code></pre>
  <p>This part loads first just the names of companies, then full companies data from the server. Each company turns into a table row. It also creates OnClientClick button events, that should load on non-first Page Load.</p>
  <pre><code>    public TableRow NewCompanyRow(HRGlobalHub.Code.Company.Company thisCompany) 
      {
          TableRow newRow = new TableRow();
  
          Button btnApply = new Button();            
          btnApply.Text = &quot;Apply&quot;;   
          btnApply.OnClientClick +=  gCompany = thisCompany; 
          btnApply.OnClientClick += Action = &quot;Apply&quot;;
  
          Button btnSuggest = new Button();
          btnSuggest.Text = &quot;Suggest&quot;;
          btnSuggest.OnClientClick += gCompany = thisCompany;
          btnSuggest.OnClientClick += Action = &quot;Suggest&quot;;
  
  
          Button btnView = new Button();
          btnView.Text = &quot;View&quot;;
          btnView.OnClientClick += gCompany = thisCompany;
          btnView.OnClientClick += Action = &quot;View&quot;;
  
          TableCell[] Cells = new TableCell[6];
  
          for (int i = 0; i &lt; Cells.Length; i++)
              Cells[i] = new TableCell();
  
          Cells[0].Text = thisCompany.Name;
          Cells[1].Text = thisCompany.FieldOfWork;
          Cells[2].Text = thisCompany.Employees.Count.ToString();
          Cells[3].Controls.Add(btnView);
          Cells[4].Controls.Add(btnApply);
          Cells[5].Controls.Add(btnSuggest);
  
          for (int i = 0; i &lt; Cells.Length; i++)
              Cells[i].Width = 150;
  
          newRow.Width = 900;
  
          for (int i = 0; i &lt; Cells.Length; i++)
              newRow.Cells.Add(Cells[i]);
  
          btnApply.Width = 150;
          btnSuggest.Width = 150;            
          btnView.Width = 150;
  
          return newRow;                       
      }
  </code></pre>
  <p>Again, since it's just testing stage for this page, the button should only display the name of the company that it gets as variable.</p>
  <pre><code>    void btnApplyClick(Object sender, EventArgs e, HRGlobalHub.Code.Company.Company thisCompany)
      {
          lblWelcomeUser.Text = &quot;Apply to &quot; + thisCompany.Name;            
      }
  </code></pre>
  <p>This works but only partly: whatever I try the code from Page Load always runs the last &quot;mentioned&quot; Action (which is &quot;View&quot;) and always goes for the company from the last row.</p>
  <p>Please help me solve this!</p>
  <p>Evgenie</p>