Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

iam working on web application in my requirement creating dynamically button and when i click an button it's worked based on button click event.

Iam creating a dynamically button but click and command event not firing

here my code is............

C#
public partial class Default11 : System.Web.UI.Page
{ 
   private Button BtnServices;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BtnServices = new Button();
        BtnServices.ID = "BtnServices";
        BtnServices.Text = "Click Me";
        BtnServices.Click += new EventHandler(BtnServices_Click);
        BtnServices.Command += new CommandEventHandler(BtnServices_Command);
        form1.Controls.Add(BtnServices);
    }

    void BtnServices_Click(object sender, EventArgs e)
    {
        Response.Write("hi");
    }

    void BtnServices_Command(object sender, CommandEventArgs e)
    {
        Response.Write("hi");
    }


}

here click event and command event not firing.

let me know were i went wrong?

thanks in advance.........
Posted
Comments
Prasad_Kulkarni 27-Jun-12 2:25am    
You've already ask'd this question: not firing button click event dynamically[^]
Don't re-post your questions, If you want to modify or update your question then use 'Improve question' widget. But don't re-post it.
REMOVE one of those.

Hi ,
Check this
C#
private Button BtnServices;

protected void Page_Load(object sender, EventArgs e)
{

        if (ViewState["falg"] != null)
        {
            Create();
        }

}

protected void Button1_Click(object sender, EventArgs e)
{
    Create();
    ViewState.Add("falg", true);
}

void BtnServices_Click(object sender, EventArgs e)
{
    Response.Write("hi");
}

void BtnServices_Command(object sender, CommandEventArgs e)
{
    Response.Write("hi");
}
void Create()
{
    BtnServices = new Button();
    BtnServices.ID = "BtnServices";
    BtnServices.Text = "Click Me";
    BtnServices.Click += new EventHandler(BtnServices_Click);
    BtnServices.Command += new CommandEventHandler(BtnServices_Command);
    form1.Controls.Add(BtnServices);
}


Best Regards
M.Mitwalli
 
Share this answer
 
Your question is already answered at not firing button click event dynamically[^]
 
Share this answer
 
Comments
U@007 27-Jun-12 2:21am    
hi Kulkarni

Once again see my code. and run it you know what's the problem.

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