473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
7 3552
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RSB | last post by:
Hi Every one, Having tuff time to make this work .. i want to have a button and on my form say Delete button. So once i click on it i want to confirm "Are you sure?" on the Client Side and if...
17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
4
by: z. f. | last post by:
Hi, I'm having an aspx page with a server form. i have a grid with a delete button and below the grid, another area with inputs for inserting new values and an "add" button for submiting the...
7
by: Alex Maghen | last post by:
I have some client-side JavaScript that I want to run whenever a pulldown <SELECT> is changes on th client. I'm trying to do this as follows... <select id="MyPulldown"...
4
by: googlegroup | last post by:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionString:SalesDBConnectionString %>" SelectCommand="<%=strSQL%>" /> In ASP.NET , how do you assign a dynamic...
11
by: 200dogz | last post by:
Hi guys, I'm making an expandable table which currently layers of <table>s are used to contain the table rows in order to show/hide them. This works, but having to format columns of the child...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.