Q:

C# ASP - Dynamic button creation and assigning event handlers

I have several dropdownlists and based on the user selection, I have to show the results retrieved from SQL in individual buttons [created dynamically]. I have a button click event which triggers the dynamic button creation.
CODE:
void CreateButtons(string val)
{
        Button btn = new Button();
        btn.Text = val;
        Panel1.Visible = true;
        btn.ID = val.Replace(" ", "_");
        btn.Click += new System.EventHandler(test);
        Panel1.Controls.Add(btn);
}

protected void Button1_Click(object sender, EventArgs e)
{
        int cont = 0;
        string[] names = new string[40];
        string del2 = "MY SQL STATEMENT"
        SqlCommand del1cmd2 = new SqlCommand(del2, conn);
        SqlDataAdapter da = new SqlDataAdapter(del1cmd2);
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach (DataRow row in dt.Rows)
        {
            names[cont] = row.Field<string>(0);
            CreateButtons(names[cont]);
        }
        Response.Write(names[cont]);
}

ISSUE:
I'm getting results from SQL, storing it on a data adapter and looping the CreateButtons function to create individual buttons for every single row returned. I'm able to get the desired result for button creation but the problem arises when I try to set an action for the buttons.
I need to set onclick event handler for all dynamically created buttons so that I'm able to give individuals actions for them.
When I click on the current code with new System.EventHandler statement, page refreshes and nothing happens, even though I have the below module.
public void test(object sender, EventArgs e)
{
     Response.Write("Dynamic Button click event");
}

RESEARCH EFFORT:
I have been looking up solutions for quite some time and I'm able to find working code with button creation in page load but haven't found a similar case with dynamic button creation on a button click event.

A:

Here's what happens.

Someone clicks Button1. This causes a PostBack
ASP.Net receives the POST request, creates a new instance (that's key) of your Page class, and begins to re-run the entire Page  life cycle.
The Button1_Click() code runs as part of the page life cycle and puts new buttons into the Page class
The Page class renders HTML to the Response, where the browser receives it and renders it to the screen for your users. At this point, the Page class instance you've been working with on the server — the one with all the new buttons — is destroyed. The buttons were rendered to the browser, though.
A user clicks on one of your new buttons. This causes a new PostBack.
ASP.Net receives the POST request and creates a new instance of the Page class.

This is where things get messy. That new instance of the Page class doesn't have any dynamic buttons. 
You need code to re-add those button into the class for every PostBack. Additionally, event handlers for the ASP.Net page life are determined before the Page_Load event. So if you wait until Page_Load to setup the dynamic buttons, things won't work right. You'll see them on the screen. You can click them and force a PostBack (because the event handler will be there for the Render step later on). But nothing will happen then you click the button.
To fix this, the buttons need to be (re)created in Page_Init or Page_PreInit.

