I made a web form in asp.net and I am using loop to add button on the page. But problem is this, I'm not able to handle event of it. Can any one help me.

Re: How to handle dynamically created buttonn's event in asp.net 80 80

Handle Command Event of button.

protected void Page_Load(object sender, EventArgs e)
    {

        for (int i = 1; i <= 3; i++)
        {

            Button btn = new Button()
            {
                Text = "Test Button" + i,
                CommandName = "Button" + i,
                CommandArgument =i.ToString()
            };
            btn.Command += new CommandEventHandler(btn_Command);
            PlaceHolder1.Controls.Add(btn);
        }
    }

    void btn_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandArgument.ToString())
        {
            case "1":
                //
                break;
            case "2":
                //
                break;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.