Friday, March 16, 2012

looping through form elements

Hi

i'm creating a table in a web application and adding row to it dynamically using javascript.
The code looks like this

<HTML>
<HEAD>
<TITLE> DOM Demo </TITLE>
</HEAD>
<BODY ID="bodyNode">

<form name="form1" method="post" runat="server" id="form1">
<input type="button" value="Add Row" onclick="addRow()">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Col 1</td>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tbody id="datatable">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
</tbody>
</table>

<script language="JavaScript">
function addRow(){
var myTbody=document.getElementById('datatable');
newRow=myTbody.firstChild.cloneNode(true);
myTbody.appendChild(newRow);
}
</script>

<asp:Button ID="Button1" runat="server" Text="Button" />

</BODY>
</HTML>

So the user can click the "Add Row" button and create a new row.
How can I loop through all the elements in the form and get back their value?

I tried this code

For Each ctrl In form1.Controls
s = s & ctrl.ID
For Each objChildControl In form1.Controls
objControl = FindChildControl(objChildControl, "form1")
If Not IsNothing(objControl) Then
s = s & objControl.ID
End If

Next
Next

It isnt getting the dynamically generated controls. Why?

Thanks a lotSet the table to runat=server, with an ID. Then after adding the new rows, try looping through it as you are now, except loop through the table. Loop through the rows collection of the table and the cells of each row.

0 comments:

Post a Comment