aspx_professional thread: Dynamic creation of ASP.NET controls which may respond to client events?
You may want to try declaring
public aspButton Button
then
Control.Add or LoadControl may work for you. A section on dynamically
loading controls is in ASP.Net Unleashed, Stephen Walther.
> Hi,
> I am trying to dynamically create a series of buttons at run-time
> (preferrably within a custom ASP.NET server control), and setup an
> eventhandler to respond to any one of these buttons being clicked.
These
> buttons will actually represent diary text entries which have been
loaded
> from a database, and clicking on an entry will allow the user to edit
it.
>
> I have been experimenting using the ASP:Literal control, which allows
you
> to dynamically alter HTML code from the server side. But, my problem is
> after I have created a dynamic link (button) to a diary entry eg:
>
> .ASPX file:
> <asp:Literal Id="TestLiteral" RunAt="server" />
>
> .cs (code-behind file):
> public Literal TestLiteral
>
> TestLiteral.Text ="<asp:Button Id='TestEntry1' Text ='12pm Team Meeting'
> Runat='Server' OnClick='HandleEdit'/>"
>
> public void HandleEdit( Object Source, EventArgs E )
> {
> /* process edit */
> }
>
> that this event does not fire as I can't dynamically create a server side
> variable to represent the button. Can anyone point me in the right
> direction to creating some controls dynamically, which can respond to
> events from the client.
>
> Many thanks,
> Richard.