Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

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

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.
BTW,

The answer for my last question was:

b.Click += new System.EventHandler(medCtrSubmit_click);
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.
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
I guess by mistake, I had accepted my own solution. My apology if this was the case.

Mike