472,334 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Need help reading controls added dynamically using JavaScript (innerHTML) in asp.net

Greetings,

I am creating a web form which will all the user to add an unlimited number
of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to
remove, and a "Save" button. When the user clicks the "Add another email" it
will call a client side JavaScript function, add_email, which will
dynamically add a new set of controls to the webpage using the innerHTML
method. It appears to work perfectly fine in the browser. The remove button
also removes the controls as expected.

The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks. Below is the code I am trying to get working.

Thanks in advance for your help.

Thanks!

Dennis
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

For Each oControl As Control In Controls
'Stop

'This is where I am trying to read the controls that were dynamically added
to the html with the JavaScript. The added controls are not in the controls
object.
Next

End Sub

</script>

<script language="javascript">

var email_count=0;

function remove_email(emailid) {

var d = document.getElementById('emailtable');

var olddiv = document.getElementById('e' + emailid);

d.removeChild(olddiv);

}

function add_email() {

email_count++;

var html='<div id="e' + email_count + '"><table><tr><td
class="FieldLabel">Type</td>' +

'<td><select id="Select1" ><option selected>Home</option><option
selected>Business' +

'</option></select></td><td class="required" colspan="2">Required</td></tr>'
+

'<tr><td class="FieldLabel">Address</td><td><input id="txtEmailAddress"
type="text" />' +

'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +

'value="-" onclick="remove_email(' + email_count + ')" /></td></tr><tr><td
colspan="4">' +

'<hr /></td></tr></table></div>';
document.getElementById("emailtable").innerHTML += html;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Test Page</title>

</head>

<body>

<form id="form1" action="Test.aspx" runat="server">

<div id="emailtable">
</div>

<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"
/>

</form>

</body>

</html>
Jan 1 '06 #1
5 3620
I am using ASP/VB.NET VS2005.

--
Thanks in advance for your help.

Dennis
"Dennis Fazekas" wrote:
Greetings,

I am creating a web form which will all the user to add an unlimited number
of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to
remove, and a "Save" button. When the user clicks the "Add another email" it
will call a client side JavaScript function, add_email, which will
dynamically add a new set of controls to the webpage using the innerHTML
method. It appears to work perfectly fine in the browser. The remove button
also removes the controls as expected.

The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks. Below is the code I am trying to get working.

Thanks in advance for your help.

Thanks!

Dennis
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

For Each oControl As Control In Controls
'Stop

'This is where I am trying to read the controls that were dynamically added
to the html with the JavaScript. The added controls are not in the controls
object.
Next

End Sub

</script>

<script language="javascript">

var email_count=0;

function remove_email(emailid) {

var d = document.getElementById('emailtable');

var olddiv = document.getElementById('e' + emailid);

d.removeChild(olddiv);

}

function add_email() {

email_count++;

var html='<div id="e' + email_count + '"><table><tr><td
class="FieldLabel">Type</td>' +

'<td><select id="Select1" ><option selected>Home</option><option
selected>Business' +

'</option></select></td><td class="required" colspan="2">Required</td></tr>'
+

'<tr><td class="FieldLabel">Address</td><td><input id="txtEmailAddress"
type="text" />' +

'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +

'value="-" onclick="remove_email(' + email_count + ')" /></td></tr><tr><td
colspan="4">' +

'<hr /></td></tr></table></div>';
document.getElementById("emailtable").innerHTML += html;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Test Page</title>

</head>

<body>

<form id="form1" action="Test.aspx" runat="server">

<div id="emailtable">
</div>

<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"
/>

</form>

</body>

</html>

Jan 1 '06 #2
I am using ASP/VB.NET VS2005.

--
Thanks in advance for your help.

Dennis
"Dennis Fazekas" wrote:
Greetings,

I am creating a web form which will all the user to add an unlimited number
of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to
remove, and a "Save" button. When the user clicks the "Add another email" it
will call a client side JavaScript function, add_email, which will
dynamically add a new set of controls to the webpage using the innerHTML
method. It appears to work perfectly fine in the browser. The remove button
also removes the controls as expected.

The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks. Below is the code I am trying to get working.

Thanks in advance for your help.

Thanks!

Dennis
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

For Each oControl As Control In Controls
'Stop

'This is where I am trying to read the controls that were dynamically added
to the html with the JavaScript. The added controls are not in the controls
object.
Next

End Sub

</script>

<script language="javascript">

var email_count=0;

function remove_email(emailid) {

var d = document.getElementById('emailtable');

var olddiv = document.getElementById('e' + emailid);

d.removeChild(olddiv);

}

function add_email() {

email_count++;

var html='<div id="e' + email_count + '"><table><tr><td
class="FieldLabel">Type</td>' +

'<td><select id="Select1" ><option selected>Home</option><option
selected>Business' +

'</option></select></td><td class="required" colspan="2">Required</td></tr>'
+

'<tr><td class="FieldLabel">Address</td><td><input id="txtEmailAddress"
type="text" />' +

'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +

'value="-" onclick="remove_email(' + email_count + ')" /></td></tr><tr><td
colspan="4">' +

'<hr /></td></tr></table></div>';
document.getElementById("emailtable").innerHTML += html;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Test Page</title>

</head>

<body>

<form id="form1" action="Test.aspx" runat="server">

<div id="emailtable">
</div>

<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"
/>

</form>

</body>

</html>

Jan 1 '06 #3
By dynamically adding the "control" on the client-side, you have no
server-side functionality built in. As I see it, you're only option appears
to be processing through the Page_Load using Request.Form information as you
would with a classic ASP application.

Another option would be using the callback features in ASP.NET 2.0. I
haven't tried these yet, so I really don't have a frame of reference with
doing callbacks at this time.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Dennis Fazekas" <df******@community.nospam> wrote in message
news:ua**************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am creating a web form which will all the user to add an unlimited
number of email addresses. Basically I have 3 buttons, "Add Another
Email", "-" to remove, and a "Save" button. When the user clicks the "Add
another email" it will call a client side JavaScript function, add_email,
which will dynamically add a new set of controls to the webpage using the
innerHTML method. It appears to work perfectly fine in the browser. The
remove button also removes the controls as expected.

The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks. Below is the code I am trying to get working.

Thanks in advance for your help.

Thanks!

Dennis
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

For Each oControl As Control In Controls
'Stop

'This is where I am trying to read the controls that were dynamically
added to the html with the JavaScript. The added controls are not in the
controls object.
Next

End Sub

</script>

<script language="javascript">

var email_count=0;

function remove_email(emailid) {

var d = document.getElementById('emailtable');

var olddiv = document.getElementById('e' + emailid);

d.removeChild(olddiv);

}

function add_email() {

email_count++;

var html='<div id="e' + email_count + '"><table><tr><td
class="FieldLabel">Type</td>' +

'<td><select id="Select1" ><option selected>Home</option><option
selected>Business' +

'</option></select></td><td class="required"
colspan="2">Required</td></tr>' +

'<tr><td class="FieldLabel">Address</td><td><input id="txtEmailAddress"
type="text" />' +

'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +

'value="-" onclick="remove_email(' + email_count + ')" /></td></tr><tr><td
colspan="4">' +

'<hr /></td></tr></table></div>';
document.getElementById("emailtable").innerHTML += html;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Test Page</title>

</head>

<body>

<form id="form1" action="Test.aspx" runat="server">

<div id="emailtable">
</div>

<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />

<asp:Button ID="btnSave" runat="server" Text="Save"
OnClick="btnSave_Click" />

</form>

</body>

</html>

Jan 1 '06 #4
> The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks.


The new controls won't be added to the ASP Controls collection as when
posting back, ASP basically only sends the values of controls on the
form; it has no way of adding new server-side control objects based on
what has been done to the DOM on the client side.

In this case, the easiest solution might be to assign a name="something
unique" to the dynamically created controls, where "something unique"
is an identifier you can predict (e.g. "email1", "email2", ...). Then,
in the server code, rather than checking through the Controls
collection, try retrieving Request["email" + n] where you start with n
= 1 and stop when it returns null / string.Empty. The values in the
request will then be the specified email address. You might have to be
careful if the user can remove addresses, as that could mean
Request["email3"] is defined but not email2; I'm sure you can figure
out some way around that, though...

Jan 1 '06 #5
Thanks for your help. You idea worked.

Here's an update to the code in case anyone else is trying to do something
similar.

Happy Year!

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim id As Integer = 1
Do While Not Request("Email" + id.ToString()) Is Nothing
Response.Write("Type: " & Request("Type" & id.ToString) & " - "
& Request("Email" & id.ToString) & "<br>")
id += 1
Loop
End Sub
</script>

<script language="javascript">
var email_inc=0;

function remove_email(emailid) {
var d = document.getElementById('emailtable');
var olddiv = document.getElementById('e' + emailid);
d.removeChild(olddiv);
}

function add_email() {
email_inc++;
var html='<div id="e' + email_inc + '"><table><tr><td
class="FieldLabel">Type</td>' +
'<td><select name="Type' + email_inc + '" id="Select1" ><option
selected>Home</option><option selected>Business' +
'</option></select></td><td class="required"
colspan="2">Required</td></tr>' +
'<tr><td class="FieldLabel">Address</td><td><input name="Email'
+ email_inc + '" type="text" />' +
'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +
'value="-" onclick="remove_email(' + email_inc + ')"
/></td></tr><tr><td colspan="4">' +
'<hr /></td></tr></table></div>';

document.getElementById("emailtable").innerHTML += html;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test Page</title>
</head>
<body>
<form id="form1" action="Test.aspx" runat="server">
<div id="emailtable">

</div>
<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />
<asp:Button ID="btnSave" runat="server" Text="Save"
OnClick="btnSave_Click" />
</form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim id As Integer = 1
Do While Not Request("Email" + id.ToString()) Is Nothing
Response.Write("Type: " & Request("Type" & id.ToString) & " - "
& Request("Email" & id.ToString) & "<br>")
id += 1
Loop
End Sub
</script>

<script language="javascript">
var email_inc=0;

function remove_email(emailid) {
var d = document.getElementById('emailtable');
var olddiv = document.getElementById('e' + emailid);
d.removeChild(olddiv);
}

function add_email() {
email_inc++;
var html='<div id="e' + email_inc + '"><table><tr><td
class="FieldLabel">Type</td>' +
'<td><select name="Type' + email_inc + '" id="Select1" ><option
selected>Home</option><option selected>Business' +
'</option></select></td><td class="required"
colspan="2">Required</td></tr>' +
'<tr><td class="FieldLabel">Address</td><td><input name="Email'
+ email_inc + '" type="text" />' +
'</td><td class="required">Required</td><td><input id="Button1"
type="button" ' +
'value="-" onclick="remove_email(' + email_inc + ')"
/></td></tr><tr><td colspan="4">' +
'<hr /></td></tr></table></div>';

document.getElementById("emailtable").innerHTML += html;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test Page</title>
</head>
<body>
<form id="form1" action="Test.aspx" runat="server">
<div id="emailtable">

</div>
<input id="btnAddEmail" type="button" value="Add Another Email"
onclick="add_email();" />
<asp:Button ID="btnSave" runat="server" Text="Save"
OnClick="btnSave_Click" />
</form>
</body>
</html>
--
Thanks in advance for your help.

Dennis
"Paul Henderson" wrote:
The problem I am having is when the user clicks the "Save" button and the
data is posted back to the server. I can't figure out how to read the
controls that were added dynamically with the JavaScript code. I am trying
to avoid a ton of postbacks.


The new controls won't be added to the ASP Controls collection as when
posting back, ASP basically only sends the values of controls on the
form; it has no way of adding new server-side control objects based on
what has been done to the DOM on the client side.

In this case, the easiest solution might be to assign a name="something
unique" to the dynamically created controls, where "something unique"
is an identifier you can predict (e.g. "email1", "email2", ...). Then,
in the server code, rather than checking through the Controls
collection, try retrieving Request["email" + n] where you start with n
= 1 and stop when it returns null / string.Empty. The values in the
request will then be the specified email address. You might have to be
careful if the user can remove addresses, as that could mean
Request["email3"] is defined but not email2; I'm sure you can figure
out some way around that, though...

Jan 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob | last post by:
Below is sample code that illustrates what I'm trying to do. For sake of brevity I didn't include the properties of buildBtn that determine what...
9
by: Ravi Singh (UCSD) | last post by:
Hello all Attached is a simple HTML file that adds and delete rows. In the add row function I set an attribute "onClick" this triggers the...
3
by: RSB | last post by:
Hi Every one Having tuff time creating web controls Dynamically. All i am trying to do is read a table and generate a list of ASP TEXT Box. So...
1
by: Frank_00001 | last post by:
I'm trying to dynamically create controls on a <DIV>; however I want to add an <HR> in between certain controls, seems simple enough. I'm having...
1
by: darwin2kx | last post by:
I have a problem that I've been tracking down for a while now. I'm working on a script that dynamically adds blocks for data entry to a page. This...
2
by: Tereska | last post by:
I want to delete script added before. I'm adding script dynamically and i'm removing later. Why it is still working? I have something like this:...
3
by: balurajeev | last post by:
Hii, currently i am working on an apliction(ASP.NET/C#) that creates dynamic html rows which contain six html text boxes and a html dropdowllist....
11
by: dhtml | last post by:
(originally mis-posted on m.p.s.jscript...) I've just closed all windows in Firefox and its using 244MB of memory. I have no idea why. I had...
4
by: omlac | last post by:
im creating controls using javascript, and i would like to retrieve the values of the controls in code behind(vb.net/c#) . My vb.net below is not...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.