Hello,
I added a button from code and with it i added onclick event.
here is the code

Button button = new Button();
button.ID = link;
button.Text = "1";
button.Click += new System.EventHandler(this.button_Click);

 private void button_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Image im = new System.Web.UI.WebControls.Image();
            im.ImageUrl = ((Button)sender).ID; //ID has image link
            Form.Controls.Add(im);
        }

When I run the page and press on the button it doesnt even run the function of button_click (checking using breakpoint..).

Why and how to fix this?

Thanks.

Hello,
I added a button from code and with it i added onclick event.
here is the code

Button button = new Button();
button.ID = link;
button.Text = "1";
button.Click += new System.EventHandler(this.button_Click);

 private void button_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Image im = new System.Web.UI.WebControls.Image();
            im.ImageUrl = ((Button)sender).ID; //ID has image link
            Form.Controls.Add(im);
        }

When I run the page and press on the button it doesnt even run the function of button_click (checking using breakpoint..).

Why and how to fix this?

Thanks.

hi

Your button id is different then in signature,
Try as below...

private void link_Click(object sender, EventArgs e)

Mark as solved if it helps you!!!!

its not working either

I tested this and it works for me.

protected void Page_Load(object sender, EventArgs e)
    {
       
            Button myButton = new Button();
            myButton.ID = "test";
            myButton.Text = "MyButton";
            myButton.Click += new EventHandler(Button1_Click);
            form1.Controls.Add(myButton);
       

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "It works!!!";
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.