By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 %>
......
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
look at Page.RenderControl(string), this will render your dynamically
created asp.net controls
"Venus" <so**@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 %>
.....

Nov 18 '05 #2

P: n/a

"Venus" <so**@gal.net> wrote in message news:WbmNc.103175$Rf.2421@edtnps84...
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
Nov 18 '05 #3

P: n/a
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
"Venus" <so**@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 %>
.....

Nov 18 '05 #4

P: n/a
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" <so**@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 %>
.....

Nov 18 '05 #5

P: n/a
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
"SMG" <SM*@nodmain.com> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
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
"Venus" <so**@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 %>
.....


Nov 18 '05 #6

P: n/a
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


"Justin Beckwith" <ju*************@gmail.com> wrote in message
news:39**************************@posting.google.c om...
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" <so**@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 %>
.....

Nov 18 '05 #7

P: n/a
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!


"Venus" <so**@gal.net> wrote in message news:<PdHNc.124653$eO.88535@edtnps89>...
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


"Justin Beckwith" <ju*************@gmail.com> wrote in message
news:39**************************@posting.google.c om...
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" <so**@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 %>
.....

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.