1

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.

2 Answers 2

1

The Dynamic controls needs to be re-created in Page_Init.

Try the example below for testing

protected void Page_Init(object sender, EventArgs e)
{
  CreateButtons("1");
  CreateButtons("2");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here's what happens.

  1. Someone clicks Button1. This causes a PostBack
  2. 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.
  3. The Button1_Click() code runs as part of the page life cycle and puts new buttons into the Page class
  4. 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.
  5. A user clicks on one of your new buttons. This causes a new PostBack.
  6. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.