Click here to Skip to main content
14,024,527 members
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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();
}
   
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
F-ES Sitecore 27-Nov-18 5:02am
   
You could simply define the variable at class level

private Button ButtonFinish;
protected void Page_Load(object sender, EventArgs e)
{
ButtonFinish = new Button()
{
ID = "btnFinish",
Text = "Finish"
};
...

However you should probably use FindControl on the placeholder to get a reference to the control, so something like

var btn = (Button)PlaceHolder1.FindControl("btnFinish");
Member 8583441 28-Nov-18 0:51am
   
For me i have created if(!IsPostBack) { CreateDynamicFinishButton(); } in page load and that is called out side page load to create a button and it is working fine.

private Button CreateDynamicFinishButton()
{
Button ButtonFinish = new Button()
{
ID = "ButtonFinish",
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(ButtonFinish_Click);
return ButtonFinish;
}

This method again called to where exactly the position it requires and it is working good but only one thing it doesn't fires the click event that's it. Please anybody suggest me sir
F-ES Sitecore 28-Nov-18 5:09am
   
You have to attach that event if it is a postback or not. Event subscriptions are not persisted in the viewstate so don't survive page refreshes.
Member 8583441 29-Nov-18 6:09am
   
Sorry sir my button click is not firing at all. I got a doubt and checked with creating a button in aspx page not in code behind and checked whether the click event fires or not but here also onclick event not fired sir
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
   

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


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190419.4 | Last Updated 28 Nov 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100