[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1229
  • Last Modified:

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 5
  • 2
1 Solution
 
anarki_jimbelCommented:
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
 
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
BTW,

The answer for my last question was:

b.Click += new System.EventHandler(medCtrSubmit_click);
0
 
anarki_jimbelCommented:
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now