Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating some buttons on a page at run time by the code
protected void Page_Load(object sender, EventArgs e)
            for (int i = 1; i <= 5; i++)
                Table tbldynamic = new Table();
                TableCell tc = new TableCell();
                TableCell tc1 = new TableCell();
                TableRow tr = new TableRow();

                Button btnSubmit = new Button();
                btnSubmit.ID = "btnSubmit"+i;
                btnSubmit.Text = "Submit "+i;
                btnSubmit.Click += new System.EventHandler(btnSubmit_click);
                pnlInfo.Controls.Add(tbldynamic);            }

now i want to fetch the id of the button on a label which i click from them how can i do that
Updated 22-Aug-12 0:30am
I.explore.code 22-Aug-12 6:20am    
you can try setting the ClientID of your controls to "Static" and that should fix the ID of the controls to whatever you define when you are creating them dynamically.

You can take the help of Sender object.

Write the code inside the button_Click method.

private void button1_Click(object sender, EventArgs e)
            Button b = sender as Button;
            label1.Text = b.Name;

Fun coding.....
Share this answer
The ClientID property of your buttons will contain the id they will have on the client side, once they have been added to the page's controls collection.
Share this answer
If you wish to get the button's ID from within the Button's click event handler, you can cast the first parameter (sender) to the Button and get it's ID like this:

Button btn = sender as Button;
string ID = btn.ID;
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