0

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.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Freon22
0

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!!!!

Edited by reach_yousuf: n/a

0

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!!!";
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.