Re: Generating dynamic form elements "<asp:button ..."

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Justin Beckwith (justin.beckwith_at_gmail.com)
Date: 07/27/04


Date: 27 Jul 2004 06:02:28 -0700

You cannot dynamically create a form element in this fashion. Because
those elements are bound to server side objects, to create them
dynamically, you need to actually instantiate a new button object, and
add it to the list of existing controls on your page.

Dim myBtn As Button
myBtn = New Button
myBtn.ID = 'myBtn'
myBtn.Text = 'Hello World!'

this code creates the button for you, and allows you to change
whatever properties you would like to change. Now you need to add the
button to an existing web control. You can add it directly to the
page collection of controls, or a panel control. For the sake of
simplicity, here is how you add it to the list of controls on the
page.

Controls.Add(myBtn)

Happy Coding!

"Venus" <some@gal.net> wrote in message news:<WbmNc.103175$Rf.2421@edtnps84>...
> Hello,
>
> I am trying to generate a dynamic form at runtime and would like to do it
> using "<asp: ..." form elements as follows
>
> Build up the string that is placed somewhere in the HTML code the same way
> like regular input fields can.
>
> strForm = "<form name=""myForm"" runat=""server"">" & vbCrLf
> strForm += "<asp:button name=""myName"" .... runat=""server"" />" & vbCrLf
> strForm += "<asp:button ....... runat=""server"" />" & vbCrLf
> strForm += "</form>" & vbCrLf
>
> --
>
> The problem this code is placed as a string "ad literam" without generating
> the elements.
> If I would use normal input fields this works fine but would like to use ASP
> controls if possible.
> Is there a way to do what I am trying to do here ?
>
> I don't want to do the code generation in the HTML section by mixing HTML
> with VB code
> <% If something %>
> <asp:button name= ... />
> <% Else %>
> .....


Quantcast