Add ASP Controls to a Table Dynamically

user489041 picture user489041 · Feb 1, 2012 · Viewed 9.9k times · Source

Right now I have an ASP Table. I can add rows and cells to this table just fine. What I would like to do, is instead of the cell just displaying text, I would like to add a control. For example a Button.

Right now, my first thought on how to do this would be just to put the <ASP:Button ... as the .Ttext attribute of the table cell. But my gut tells me this wont work. Further more, I probably couldn't add a function to handle the button click.

Can someone help point me in the right direction on how to achieve this?

Answer

Paul picture Paul · Feb 1, 2012

You need to add the control to the table cell. Just call the Controls.Add method on the cell and add your control. Below is a brief sketch that should point you in the right direction.

        Button b = new Button();
        c.Controls.Add(b);