Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating text box from list box - all created via a loop 2

Status
Not open for further replies.

rabisco

MIS
Jan 4, 2007
21
I am attemting to update multiple textboxes with the value of a selected option in multiple list boxes. Each list box has a text box next to it.

My code is a follows.....

<td>
<SELECT
name="excelValues" onChange="document.frmMap.columnValue.value=this.options[this.selectedIndex].text">
<OPTION>None</OPTION>
<% for X = 0 To miaRecordSet.Count - 1 %>
<OPTION><%= miaRecordSet.Item(X).Name %></OPTION>
<%
Next
%>
</SELECT>
</td>
<td>
<input readonly="readonly" name="columnValue" class="MediumText"></input>
</td>
<%
next
%>

The listbox is created by another for each loop further up the page. so , for 5 elements found in a database, 5 options get created. Now I need to populate each text box, with the value of the selected option in the list box.

Any help would be appraciated.



 
so what exactly is going wrong with your code? As far as i can see it should work fine.
 
The text box is not getting updated when I select an option in the list box.

Please note that the number of list boxes and text boxes are determined by a for each routine further up the page.

If I remove the text box from the second for each loop i.e. so I have only one text box, each of the list boxes can update it.
 
Oh ok, well u might get better help by posting the src of your html page(right click and view src), so we can see what is either throwing javascripting error or "not working". Also javascript for the most part doesn't care how the select list is made. What i would do to get your your code to work is verify that select lists and text input are in your form tags if your gonna reference via(document.frmMap), After give each of your select lists a UNIQUE id or name. Retry your code using the id or name. If it doesn't work post ur rendered html with out server side code.
 
Code:
<html>
	<script type="text/javascript">

	</script>
	<body>
	<form name="frmMap">
		<SELECT name="excelValues1" onchange="document.frmMap.columnValue1.value=this.options[this.selectedIndex].text">
			<OPTION>None</OPTION>
			<OPTION value="1">1</OPTION>
			<OPTION value="2">2</OPTION>
			<OPTION value="3">3</OPTION>
		</SELECT>
		<input readonly="readonly" name="columnValue1" class="MediumText"></input>
		<SELECT name="excelValues2" onchange="document.frmMap.columnValue2.value=this.options[this.selectedIndex].text">
			<OPTION>None</OPTION>
			<OPTION value="1">1</OPTION>
			<OPTION value="2">2</OPTION>
			<OPTION value="3">3</OPTION>
		</SELECT>
		<input readonly="readonly" name="columnValue2" class="MediumText"></input>
		<SELECT name="excelValues3" onchange="document.frmMap.columnValue3.value=this.options[this.selectedIndex].text">
			<OPTION>None</OPTION>
			<OPTION value="1">1</OPTION>
			<OPTION value="2">2</OPTION>
			<OPTION value="3">3</OPTION>
		</SELECT>
		<input readonly="readonly" name="columnValue3" class="MediumText"></input>
		<SELECT name="excelValues4" onchange="document.frmMap.columnValue4.value=this.options[this.selectedIndex].text">
			<OPTION>None</OPTION>
			<OPTION value="1">1</OPTION>
			<OPTION value="2">2</OPTION>
			<OPTION value="3">3</OPTION>
		</SELECT>
		<input readonly="readonly" name="columnValue4" class="MediumText"></input>
	</form>
	</body>
</html>
and easy way to do this with ur code would be to append the loop index to ur html element names and update ur javascript code. Make it render like the example above.. hope that helps.
 
But the issue here is that the number of selected boxes to be created will not always be the same, so I cannot name them.


The list boxes are and text boxes created dynamically at run time.
 
If the listbox is created by a for each loop further up the page what is stopping you from renaming them during thier creation in the for each loop?
I guess if you can't rename then your next option is to put your select lists and text inputs into a div with a unique id, 'divid'.
Code:
	var selects = document.getElementById('divid').getElementsByTagName("select");
	var inputs = document.getElementById('divid').getElementsByTagName("input");
	for(i=0; i<selects.length){
		inputs[i].value = selects[i].text;
	}
it would be alot easier to do my first suggestion though.
 
Name the fields as you create them. Create the variable "i" and increment it at the beginning of the outer loop.
Code:
<td>
<SELECT 
name="excelValues_[b][red]<%=i%>[/red][/b]" onChange="document.frmMap.columnValue_[b][red]<%=i%>[/red][/b].value=this.options[this.selectedIndex].text">
<OPTION>None</OPTION>
<% for  X = 0 To miaRecordSet.Count - 1 %>
<OPTION><%= miaRecordSet.Item(X).Name %></OPTION>
<%
Next
%>                
</SELECT>    
</td>
<td>
<input readonly="readonly" name="columnValue_[b][red]<%=i%>[/red][/b]" class="MediumText"></input>
</td>        
<% 
next
%>

Adam
 
Thanks.I'll try this out straight away.

so where im my code to I create the i variable and incrementing it..

the outer for loop is posted below.....

</head>
<form name="frmMap" ID="frmMap" ACTION="MapMetadata2.asp?<%= Request.ServerVariables("QUERY_STRING") %>">
<table>
<tr>
<td colspan="4">You have selected the <b><%= objMdGroup.Name %></b> metadata group</td>
</tr>
<tr>
<td colspan="4">You have selected the file : <b><%= Request.Form("excelFile") %></b> </td>
</tr>
<tr>
<td>
<b>Metadata</b>
</td>
<td>
<b>Data Type</b>
</td>
<td>
<b>Excel Column Value</b>
</td>
<td>
<b>Value</b>
</td>

</tr>
<!-- Insert Loop of Metadata group items Here vvv -->

<%
for each objMetaDef in objMembers

Dim objMetaDefTypeN, objMetaDefTypeA
objMetaDefTypeN = objMetaDef.Type

'convert metadata type from numeric to alpha.
if (objMetaDefTypeN = 1) then
objMetaDefTypeA = "Boolean"
elseif (objMetaDefTypeN = 3) then
objMetaDefTypeA = "Value List "
elseif (objMetaDefTypeN = 6) then
objMetaDefTypeA = "Text"
elseif (objMetaDefTypeN = 7) then
objMetaDefTypeA = "String List"
elseif (objMetaDefTypeN = 18) then
objMetaDefTypeA = "Compound "
end if
%>
<tr>
<td><%= objMetaDef.Name %></td>
<td align="middle"> &nbsp;<%= objMetaDefTypeA %></td>

<%

'Routines for fetching excel file from file system
Dim miaConn
Dim miaRecordSet
Dim file
Set file = request.form("excelFile")
Set miaConn = Server.CreateObject("ADODB.Connection")
Dim miaConnString
miaConnString = "DRIVER={Microsoft Excel Driver (*.xls)};DBQ=" & file &"; ReadOnly=0"
miaConn.Open(miaConnString)
miaRecordSet = miaConn.Execute("Select * FROM [MapData];")

%>
 
Thank adam0101 and j4606.

You have made my day.

Have a great weekend, and I hope the weather where you are is better than it is in Chicago.
 
One more question.

I need to use the values populated into the text boxes to update an application. Any tips?
 
This has sort of turned into an ASP question, but what I've done in the past was to loop through your Request.Form collection on the server-side and look for any field name that starts with "columnValue_". Parse out the number that follows it and concatenate it to the end of "excelValues_" to get the name of the matching select box.

Adam
 
Thanks adam0101. Could you kindly post a sample?
 
adam0101 I'll appreciate it if you could post some code samples
 
Sorry, I'm in training this week so I'm not able to spend much time here. I haven't worked with classic ASP in a while but it would be similar to something like this:
Code:
For Each field in Request.Form
  If InStr(field, "columnValue_") = 0 Then
    colVal = Request.Form(field)
    id = Right(field, Len(field) - InStr(field, "_"))
    xlVal = Request.Form("excelValues_" + id)

[green]    'Do something with the colVal and xlVal variables[/green]
  End If
Next

Adam
 
Thanks - enjoy your training.

It's been a while I've touched asp. This client just wanted some customization done to an asp application.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top