Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
2.64/5 (3 votes)
See more:
Hiii.
I have created 5 dynamic buttons on a click of a button.Now i want that the newly created dynamic buttons must have a click event and perform some operation on their click
I have done this in C# windows forms
Unable to do in asp.net (C#)
I have taken a panel in asp and then with the click of a button i have created some dynamic buttons.I have tried the Click+= event handler too but with no success
My code till now

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        createbuttons();
    }

    public void createbuttons()
    {
        for (int i = 2; i <= 5; i++)
        {
            Button b = new Button();
            b.ID = "Button"+i.ToString();
            b.Text = "Button"+ i.ToString();
            b.Click+= //problem lies here...i did something here in the event handler like
                        Response.Write((Button)Panel1.FindControl(ID).Text;

            Panel1.Controls.Add(b);
        }
    }
}
Posted
Updated 29-Dec-17 0:43am
v3

 
Share this answer
 
Hi,
Try this out:
C#
Button btnDyn;
Label lbl;

protected void Page_Init(object sender, EventArgs e)
{
    btnDyn = new Button();
    btnDyn.ID = "btnDyn";
    btnDyn.Style["Position"] = "Absolute";
    btnDyn.Style["Top"] = "100px";
    btnDyn.Style["Left"] = "10px";
    btnDyn.Click += new EventHandler(Button_Click);
    this.form1.Controls.Add(btnDyn);
 
    lbl = new Label();
    lbl.ID = "lblDyn";
    lbl.Style["Position"] = "Absolute";
    lbl.Style["Top"] = "150px";
    lbl.Style["Left"] = "10px";
    this.form1.Controls.Add(lbl);
}

protected void Page_Load(object sender, EventArgs e)
{
    btnDyn.Text = "Dynamic Button";
    lbl.Text = "";
}

protected void Button_Click(object sender, EventArgs e)
{
    lbl.Text = "dynamic label text";
}
 
Share this answer
 
v2
C#
public void createbuttons()
   {
       for (int i = 2; i <= 5; i++)
       {
           Button b = new Button();
           b.ID = "Button"+i.ToString();
           b.Text = "Button"+ i.ToString();
           b.Click += new EventHandler(b_Click);
           this.form1.Controls.Add(b);
           Panel1.Controls.Add(b);
       }
   }


C#
protected void b_Click(object sender, EventArgs e)
    {
        //Your process you want to do on click.
    }


Accept as answer if solve you problem.
 
Share this answer
 
Comments
Rajan Maheshwari 8-Jul-13 2:23am    
protected void b_Click(object sender, EventArgs e)
{
Button b=sender as Button;
Label1.Text = ((Button)Panel1.FindControl("Button1")).Text;
}

I did this but didnt worked
jaideepsinh 8-Jul-13 2:43am    
Write this where you want to call click event.
b.Click += this.b_Click;
Rajan Maheshwari 8-Jul-13 3:03am    
can you please elloborate because b_click is now a function which requires 2 arguments
Member 11754700 10-Jun-15 6:43am    
try this i think it's work ..
bt.Click += new EventHandler(bt_click);
Shrikesh_kale 19-Aug-15 8:53am    
how to download pdf for each button click,i.e. if buttonID is Button1 then button1.pdf must be download and so on....
Put dis link button creation code in Page load with out
if (!Page.IsPostBack)

and write handler for it
This will work

LinkButton lbtnDownload = new LinkButton();
                  lbtnDownload.Click += new System.EventHandler(lbtnDownlaod_Click);
                  lbtnDownload.Text = "Downlaod Here";
                  Div.Controls.Add(lbtnDownload);


 private void lbtnDownlaod_OnClick(object sender, EventArgs e)
        {
//Your code here 
}
 
Share this answer
 
Comments
CHill60 29-Dec-17 8:56am    
Answered over 4 years ago! You've added nothing new to previous solutions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900