add event for dynamically generated buttons... asp.net c#

I have the following event:

protected void medBtn_Click(object sender, EventArgs e)
    {
       // do stuff
    }

I want to include with the following code which generates number of buttons as required.

Question: Could you possibly show me how to add this click event to all the buttons the code generates?

Thank you.
    static Panel myPanel2 = new Panel();
    static string r_name = "Region 1";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Region"] == null)
            Session["Region"] = "Region 1";
        r_name = (string)Session["Region"];//.ToString();
        //String rName = rn;
        Panel myPanel = new Panel();
        myPanel = this.MedCenterPanel;
        myPanel2 = myPanel;

        using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
        {
            // Create the command and set its properties.
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "SELECT .. ORDER BY ...";
            command.CommandType = CommandType.Text;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int id = reader.GetInt32(0);
                    string btnCaption = reader.GetString(1);
                    makeButtonFor(id, btnCaption);
                }
            }
            else
            {
                // so something else
            }

            reader.Close();
        }
    }
    static private void makeButtonFor(int id, string caption)
    {

        Button b = new Button();
        {
            b.ID = "medButton" + id.ToString();
            b.Text = caption;
            var captionButton = (caption.Substring(0, 6) == "Region") ? "RegionButton" : "medCtrButtonStyle";
            b.ControlStyle.CssClass = captionButton;
            myPanel2.Controls.Add(b);
        };
    }

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
You need to associate an event handler with your dynamically created button.Something like below:

protected void Page_Load(object sender, EventArgs e)
{
   ...
   myButton.Click += new System.EventHandler(btnSubmit_click);
  ...
}

// Dynamic button click event
protected void btnSubmit_click(object sender, EventArgs e)
{
   // do what you need
}

Open in new window


http://www.aspdotnet-suresh.com/2012/07/create-controls-dynamically-in-aspnet.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Sorry for the delay,

I added your solution as line 10, but at the end of the line at "medCtrSubmit_click()", I got an error. The error complains about the missing arguments in "medCtrSubmit_click()":

'No verload for method 'medCtrSubmit_Click() takes 0 argument.'

I tired the following but it didn't work:

b.Click += new System.EventHandler(medCtrSubmit_click(Object sender, EventArgs b.ID));

Question: What the syntax for the argument ought to be?
 
    private void makeButtonFor(int id, string caption)
    {
        Button b = new Button();
        {
            b.ID = "medButton" + id.ToString();
            b.Text = caption;
            var captionButton = (caption.Substring(0, 6) == "Region") ? "RegionButton" : "medCtrButtonStyle";
            b.ControlStyle.CssClass = captionButton;
            myPanel2.Controls.Add(b);
            b.Click += new System.EventHandler(medCtrSubmit_click());
        };
    }

    private void medCtrSubmit_click(object sender, EventArgs e)
    {
        // do what you need
    }

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for eghtebas's comment #a40256286

for the following reason:

Thank you.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
BTW,

The answer for my last question was:

b.Click += new System.EventHandler(medCtrSubmit_click);
0
anarki_jimbelSenior DeveloperCommented:
The answer was correct - how to add an event handler to a dynamically created button. The author used the solution. Exact syntax could be found in textbooks.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re: Exact syntax could be found in textbooks.

This fine but it didn't work, but the following worked.

b.Click += new System.EventHandler(medCtrSubmit_click);

perhaps because the solution provide was for generic events requiring no parameters.

now, all is good and I appreciated for your input.

Regards,

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I guess by mistake, I had accepted my own solution. My apology if this was the case.

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.