Well, the scope of your button is not correct.
You need to declare the button as part of the page, you cannot declare the button within a function and expect it to work. If you do, then the button will render on the page but the life span of the button is within the function so any events (like the
click event) that the button does may not actually be detected properly.
Another thing is that you have to instantiate the button in the PageInit() event. The reason is because the PageInit event occurs just before the ViewState for the page is loaded. When the ViewState is for the page is loaded, the ViewState for all of the controls on the page is loaded.
The ViewState contains state information about the control. If your dynamic control was a TextBox, the text that the user entered into the TextBox is loaded into the Server Side TextBox object when the ViewState is loaded for this dynamic TextBox happens.
Likewise, if you have a dynamic button and the user clicks the button, the button click event is detected when the ViewState is loaded for the button control.
If you do not instantiate your dynamic controls in the Page Init event then any events or other ViewState data will not properly be loaded and your code wont work as you expect it.
If you need to get the control by ID then you need to give the control an ID after you instantiate it.
Please see this article for more information:
http://bytes.com/topic/net/insights/...ntrols-asp-net
The repeater is actually a very good idea because it uses the
INamingContainer interface. This interface will properly assign unique IDs to every dynamic control within the control that implements it. That is why Semomaniz's suggestion about using a
Repeater is such a good idea.
Another control that implements the INamingContainer is the
GridView control.
Both the Repeater and the GridView can be used to specify controls (like TextBoxes, Labels, Buttons, LinkButtons etc) that should be used to display and interact with the data that the GridView/Repeater is bound to. You do this using something referred to as a "template"
This means that all you have to do is create your table. This table will become the Data Source for the GridView or the Repeater. When you use the DataBind() method on the GridView or the Repeater the data will be displayed according to the template that you have outlined in the GridView/Repeater. This is particularly nice because it keeps a clear separation between the User Interface and the Data layers. Using this technique the user interface and data source are not merged together. It makes life a lot easier for maintaining your application in the future.
If your GridView/Repeater has a button in it's template, then each row of data will have a button assigned to it (dynamically). The controls within the template are all given unique IDs because both the GridView and the Repeater implement the INamingInterface. When the user clicks one of these buttons you can determine what row that the button belongs to etc etc.
-Frinny