Click here to Skip to main content
15,899,825 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Questionhow to handle dynamicaly created button in asp.net with c#?? Pin
yogesh_softworld12317-Dec-07 1:33
yogesh_softworld12317-Dec-07 1:33 
AnswerRe: how to handle dynamicaly created button in asp.net with c#?? Pin
Abhijit Jana17-Dec-07 2:16
professionalAbhijit Jana17-Dec-07 2:16 
GeneralRe: how to handle dynamicaly created button in asp.net with c#?? [modified] Pin
yogesh_softworld12317-Dec-07 3:20
yogesh_softworld12317-Dec-07 3:20 
GeneralRe: how to handle dynamicaly created button in asp.net with c#?? Pin
Abhijit Jana17-Dec-07 19:10
professionalAbhijit Jana17-Dec-07 19:10 
Hi yogesh,
Use this one
public partial class _Default : System.Web.UI.Page 
{
    Button[] b = new Button[10]; //Create Array of buuton

    protected void Page_Load(object sender, EventArgs e)
    {

        for (int i = 0; i < 10; i++)
        {
            b[i] = new Button(); //Create Button
            //b[i].ID = i;  //Can use ID
            b[i].Text = "Dynamic Button  " + i.ToString() ;
            Panel1.Controls.Add(b[i]);
            //Add Event Handler to that Dynamic button
            b[i].Click += new System.EventHandler(mybtn_Click);
            
        }
    }

    public void mybtn_Click(object sender, EventArgs e)
    {
        //Create a Button that it received
        Button mybtn =sender as Button;
        //You can Do any thing here !!!
        Response.Write(mybtn.Text.ToString());
        // you can store session over here
    }
}


This will solve your problem !!!

Best Regards
-----------------
Abhijit Jana
View My CodeProject Articles

"Success is Journey it's not a destination"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.