Click here to Skip to main content
14,735,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

iam creating a button dynamically and generate click event

actually my functionality is creating button based on services(count) and generate click event
here im creating gloabally object.

buttons are creating based on count but when i click the button click event not firing.

but it's not firing let me know any issue.

//global
private Button BtnServices;

//button creating for loop
       BtnServices = new Button();
       BtnServices.Text = "Click Me";
       BtnServices.Click += new EventHandler(BtnServices_Click);

void BtnServices_Click(object sender, EventArgs e)
    {
          Label1.Text="Hi...";
    }
Posted
Updated 25-Jun-12 23:52pm
v2
Comments
johannesnestler 26-Jun-12 7:42am
   
where is the button added, your code should work, problem must be elsewhere

Try this:
BtnServices = new Button();
BtnServices.ID = "BtnServices";
BtnServices.Text = "Click Me";
BtnServices.Click += new System.EventHandler(BtnServices_Click);

void BtnServices_Click(object sender, EventArgs e)
    {
          Label1.Text="Hi...";
    }
   
Comments
U@007 26-Jun-12 4:49am
   
it's not working prasad..
Prasad_Kulkarni 26-Jun-12 9:57am
   
Is it throwing any error??
Hi,

Just by assigning a delegate, the event will not fire. You must invoke the delegate.

you must render this button to the aspx page, you must handle the view state, then when user clicks on the button, then the event will fire.

if you are not rendering the button, how user will click on it..??

if you are not rendering the button and if you want that functinality, just call that function like this.

BtnServices_Click(BtnServices, new EventArgs();


Happy Coding.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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