PDA

Click to See Complete Forum and Search --> : Finding the name of OBjects


lenin
May 24th, 2000, 03:00 AM
Hi,
Is anyone aware of how to request the name of each object on a HTML form?

I am dynamically creating textboxes based on the name of a field returned from an SQL query, however, when I try to access the contents of the text boxes with the name I think I gave them, it returns nothing despite the fact they are populated. Code to create the text boxes, with values below.

dim num
num = 1
do while not records.eof
for each fld in records.fields
response.write "<TD align='center' >"
if fld.name = "quantity" then
response.write "<input type = 'text' size='5' name='quantity" & num & "' value = " & fld.value & ">"
num = num + 1
else
response.write "<font face='Arial'>" & fld.value & "</font>"
end if
response.write "</TD>"
next
total = total + records("Total")
response.write "<tr>"
records.movenext
loop


Any help appreciated.

Lenin

May 24th, 2000, 07:31 AM
Not sure why you're trying to do what you've listed but here's some code that spins through all of the employees in the employees table in the pubs database. Keep in mind that when you do this you're creating arrays of each field. The button I placed at the end of the script will give you the length of the "LastName" array you've created by building the page this way. If you're considering building an updatable form this way I would advise against it(this is the only reason i can see for doing this...).


<%
Response.Buffer = true
Response.Write ("<html><body background="/images/bground.gif" bgcolor="#ffffff" text="#000000" marginheight="0" topmargin="0">")
dim recData
dim conData
dim strConnectionString
dim fld


strConnectionString = "provider=sqloledb;user id=sa;password=;initial catalog=northwind;server=MyServer;"
set conData = Server.CreateObject ("ADODB.Connection")
conData.ConnectionString = strConnectionString
conData.Open
set recData = Server.CreateObject ("ADODB.Recordset")
recData.Source = "select * from employees"
recData.ActiveConnection = conData
recData.Open
if not (recData.BOF and recData.EOF ) then
Response.Write ("<form name=test method=post action=somepage.asp>")
do while not recData.EOF
for each fld in recData.Fields
%>
<input type="text" size="50" name="<%= fld.Name %>" value="<%= fld.Value %>"><br>
<%
next
Response.Write ("<hr><br>")
recData.MoveNext
loop
else
Response.Write ("no records found")
end if
Response.Write ("</form>")
%>
<input type="button" value="test" onclick="alert(document.forms.test.LastName.length)">
<%
Response.write ("</body></html>")


'clean up recordset
if not recData is nothing then
if recData.State > 0 then recData.Close
set recData = nothing
end if

'clean up connection
if not conData is nothing then
if conData.State > 0 then conData.Close
set conData = nothing
end if
%>


To access any one of the text fields you will have to access the array of elements your script built for each field in the recordset. So if you wanted to access the first LastName element(as in the example above) it would be like this:

var strLastName = document.forms.test.LastName[0].value;

this is a total pain in the ass, don't do it. I would do a query, build a list of the results with the option to edit any ONE record and go from there.

just my two cents,
pvb

lenin
May 24th, 2000, 04:09 PM
Thanks for the reply PVB, you are right I want to update values in a table with those currently in the text boxes. Instead of searching for changes I want to call the update function whether the boxes have changed or not.

I am not interested in messing with arrays, I thought that using some sort of control collection to go through all the controls on the form and if a control was a text box then call an SQL update command with the contents of the text box and a parameter.

My problem is that I don't know if the text boxes I have created have the names thet should have, hence me my.

Mark Sreeves
May 24th, 2000, 04:35 PM
is this the same problem as you had yesterday?

http://forums.vb-world.net/showthread.php?threadid=17418

lenin
May 24th, 2000, 04:38 PM
not exactly, however it is the same project. I had a general query regarding finding the names of controls on a form, which coincided with this issue.

Mark Sreeves
May 24th, 2000, 05:05 PM
<%@ Language=VBScript %>
<html>
<head>

</head><body>
<%
dim num
dim i

for i = 1 to Request.QueryString.Count
Response.Write(Request.QueryString.Item(i) & "<BR>")
next

%>

<form action="scratch.asp">

<%
for num = 1 to 5
response.write "<input type = 'text' size='5' name='quantity" & num & "' value = " & num * 100 & "><BR>"
next
%>

<input type=submit value="Save" id=submit1 name=submit1>
</form>

</body>

</html>