Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My asp.net button is created at code behind and i require onclick event should be generate. This onclick event is created but not firing when button clicks at run time. I have given break point at opening braces of button click event

What I have tried:

Button ButtonFinish = new Button()
{
    Text = "Finish"
};
ButtonFinish.Attributes.Add("class", "btn btn-group-toggle btn-primary");
ButtonFinish.Style["margin-bottom"] = "10px";
ButtonFinish.Attributes.Add("runat", "server");
ButtonFinish.Click += new EventHandler(this.ButtonFinish_Click);

private void ButtonFinish_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}
Posted
Updated 27-Nov-18 20:02pm

You have to make sure you attach to the click event on page load. In fact you'll probably have to do all of your ButtonFinish code on page load

// Make sure this code is in your page_load event.  Also give the button an ID
Button ButtonFinish = new Button()
{
    Text = "Finish"
};
ButtonFinish.Attributes.Add("class", "btn btn-group-toggle btn-primary");
ButtonFinish.Style["margin-bottom"] = "10px";
ButtonFinish.Attributes.Add("runat", "server");
ButtonFinish.Click += new EventHandler(this.ButtonFinish_Click);


Here's an example with context, you need a place on the page for your button to appear, so add a PlaceHolder in the location you want to show the button

<asp:PlaceHolder ID="PlaceHolder1" runat="server" />


The code-behind looks like this

protected void Page_Load(object sender, EventArgs e)
{
    Button ButtonFinish = new Button()
    {
        ID = "btnFinish",
        Text = "Finish"
    };
    ButtonFinish.Attributes.Add("class", "btn btn-group-toggle btn-primary");
    ButtonFinish.Style["margin-bottom"] = "10px";
    ButtonFinish.Attributes.Add("runat", "server");
    ButtonFinish.Click += new EventHandler(this.ButtonFinish_Click);

    PlaceHolder1.Controls.Add(ButtonFinish);
}

private void ButtonFinish_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}
 
Share this answer
 
v2
Comments
Member 8583441 26-Nov-18 12:14pm    
object instance not created error is coming sir
F-ES Sitecore 26-Nov-18 12:35pm    
On what line?
Member 8583441 26-Nov-18 12:17pm    
and also whole page is collapsed means web application full of white screen this error now what should i do sir
F-ES Sitecore 26-Nov-18 12:44pm    
I've updated my solution with a basic example of adding a dynamoc button
Member 8583441 27-Nov-18 0:19am    
Your solution is accepted but one error is occurred that is when i initialize button event in page load. But i require that button to be used in outside page load. How can i achieve that
See if this hack works for you: Add Dynamic Buttons and Handle click event on server side in ASP.NET - TechBrij[^]

PS: You need to understand that you need to wire-up the event handler on each and every postbacks for dynamically created controls.
 
Share this answer
 

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