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

V

Venus

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 %>
......
 
S

Steve Flitcroft

look at Page.RenderControl(string), this will render your dynamically
created asp.net controls
 
H

Hans Kesting

Venus said:
Hello,
[snip]

strForm = "<form name=""myForm"" runat=""server"">" & vbCrLf
strForm += "<asp:button name=""myName"" .... runat=""server"" />" & vbCrLf
strForm += "<asp:button ....... runat=""server"" />" & vbCrLf
strForm += "</form>" & vbCrLf

Just an offtopic note: if you are going to do a lot of "string += otherstring"
statements, consider using StringBuilder instead. It's a lot faster for
string concatenation.

Hans Kesting
 
S

SMG

Dear Vinus,

Here is the Code:::: ( You can copy paste the below given code in
page_Load )
StringBuilder strBuild= new StringBuilder();
strBuild.Append("<asp:Label id=one ");
strBuild.Append(" runat=server>Hello!!! Can you see me");
strBuild.Append("</asp:Label>");
Page.Controls.Add( Page.ParseControl(strBuild.ToString()));

// Second Way of Coding... Create Your Control on the Fly

Label lblName = new Label();
lblName.ID = "NewID";
lblName.Text = "My New Label to display ";
Page.Controls.Add(lblName);

Regards,
Shailesh MG
 
J

Justin Beckwith

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!
 
V

Venus

Hello,

Thanks for your reply.
I understand that a control can be created dynamically in several ways:
1) using StringBuilder
2) using Controls.Add
3) using ASP PlaceHolder

But this is just for the controls and not for the form itself.
What I am trying to achieve is to create an entire form (including controls)
dynamically and place it in HTML when needed.
So my next question would be how to do this ? In other words how to place
the control elements in the dynamic form ?
Assuming of course that the controls have been generated using one of the
above methods.

strForm = "<form name=""someName"" method= .....>"
----------------------------------------------------------
how to place the elements inside the form ???
----------------------------------------------------------
strForm += "</form>"
=========================================================
and now place it in HTML

<td><%=strForm %> </td>
=========================================================

Thanks
 
V

Venus

So if I do as you say, how does the control knows which form it should bind
to ?
If the page consists of two forms (let's say form1 and form2) ?

Controls.Add is just adding the control to the page and not a specific form
 
J

Justin Beckwith

In the general case, you are only going to have one form element on
your page in ASP.NET. But in the rare event you are maintaing two
forms, you could just get that form element, and add the control to a
sepecific form.

Example:
Me.FindControl("Form1").Controls.Add(myBtn)

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top