473,385 Members | 2,028 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,385 software developers and data experts.

filling a textbox corresponding to a value selected in listbox

Hi all,
I am new to ASP, i am making a page on which i have a list box and a textbox.
I have dynamically populated the list box from data on the server, what i want it that , when i select a option on the list box the corresponding value should come on the text box on its own, without having to press any other button..

eg : i have a table on the database with fields
'course name'
'course cost'
so when i select the course name from the listbox, the textbox should show the cost of the course.

What i figured out after googling for it was that i will have to reload the page on the "on change event " of the list box and get the value and fill the text box.didn't really understand that well.

Can somebody please tell me, how is that suppose to be done ??

I know Vbscript yet..

Thank's.......
Mar 4 '09 #1
3 7510
Hi there aashishn86!

Sounds like you are going to need some client side scripting. In other words, unless you are using a .NET postback, you will need to have assign the listbox an onchange event that will call a j/s function to populate your text box.

The j/s for this wouldn't be too tough. Assuming the following(just for demonstration purposes):

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. myDynamicallyPopulatedValue = "Test"
  4.  
  5. Response.write "<select name='mySelect' id='mySelect' onchange='updateUI(this.value);' value=''>"
  6. Response.write "<option value='" & myDynamicallyPopulatedValue & "' >" &  myDynamicallyPopulatedValue & "</option>"
  7. Response.write "</select>"
  8. Response.write "<input type='text' name='myTargetBox' id='myTargetBox' value='' />"
  9.  
  10. %>
  11.  
Now for the j/s to make it work:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function updateUI(userValue)
  3. {
  4. var userSelection = userValue;
  5. var targetBox = document.getElementById("myTargetBox");
  6. targetBox.value = userSelection;
  7. }
  8. </script>
  9.  
Something like that should work. Not sure you have a lot of options for performing a client-side browser update without refreshing the document.window(probably not good) or using client-side controls.

Best Regards,
Jenkins
Mar 4 '09 #2
Expand|Select|Wrap|Line Numbers
  1. <%@ LANGUAGE="VBScript" %> 
  2. <html>
  3. <body>
  4.  
  5. <script type="text/javascript"> 
  6. function updateUI(userValue) 
  7. var userSelection = userValue; 
  8. var targetBox = document.getElementById("myTargetBox"); 
  9. TargetBox.value = userSelection; 
  10. </script> 
  11.  
  12.  
  13.  
  14. <%
  15.   strcon = "provider=sqloledb;data source=1.234.5.678,1234;initial catalog = abcde; User Id=abcdef ;password=abcde"
  16.   Set objcon = Server.CreateObject("ADODB.Connection")
  17.   objcon.ConnectionString = strcon
  18.   objcon.Open
  19.  
  20.   strsql = "select cname from test3"
  21.  
  22.   Set objrset = Server.CreateObject("ADODB.Recordset")
  23.       objrset.Open strsql,strcon,2,3,1 %>
  24.  
  25.  
  26. <select name = "select one" onchange= 'updateUI(this.value)' >
  27. <% while not objrset.eof %>
  28. <option value = "<%=objrset("cname")%>"><%=objrset("cname")%></option>
  29. <%objrset.movenext
  30.   wend %> 
  31. </select> 
  32.  
  33.  
  34. <input type = "text" size="20" name="mytargetbox">
  35.  
  36. </body>
  37. </html>
  38.  
this is what i am writing..
it doesn't seem to work .
the error i get is .
"targetbox is undefined"
line 10 column 1

i am using classic asp...
Mar 5 '09 #3
Ok, just so you are aware, javascript is case sensitive. Change your function to this:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> 
  2. function updateUI(userValue) 
  3. //Remove the alert line once you have confirmed you are getting the correct value.
  4. alert (userValue);
  5. var userSelection = userValue; 
  6. var targetBox = document.getElementById("mytargetbox"); 
  7. targetBox.value = userSelection; 
  8. </script> 
  9.  
The undefined error was a result of the DOM not being to locate the element b/c of differences in the case. It is a literal string comparison.

Let me know if you require additional assistance.

Best Regards,
Jenkins
Mar 6 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fabrice | last post by:
Hi, I have a trouble with my TextBox Control and it's ViewState in my form... The property for it, is FALSE as following : <form id="frmSearchBre4" runat="Server"> <asp:textbox id="myID"...
1
by: lance2001 | last post by:
Hi, After viewing the entire Visual Basic 2005 Express Edition for Beginners video series, I have begun building a database driven application that will make use of listbox1 (multi-select)...
1
by: sanjrose | last post by:
Hi, I hav got three fields in a UI where the first field is a listbox with values fetched from the database and the second,third fields are textboxes which should get their corresponding value...
7
by: technocraze | last post by:
Hi guys, I encountered this error while using the AfterUpdate event for my listbox. Error: Update or CancelUpdate without using AddNew or Edit. What i wanted to achieve is just to display the...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
1
by: Peter | last post by:
Hi, I'm trying to create a form that shows table rows in a listbox. Several comboboxes expand the foreign key fields into text values from the parent tables, and there are also some textboxes...
2
by: remya1000 | last post by:
i'm using VB.NET. i have a table names "EMP". this table contain employee details like Index, empID, First name, Last name, address, phone number etc. while page loads, i need to display Last...
9
Vini171285
by: Vini171285 | last post by:
Hi, Actually i am filling a list box from database and depending on change in that list box ,the value corresponding to that selected index should come in the textbox,which should come from another...
2
by: E11esar | last post by:
Hi there. I am looking to use a dataset return from a webservice that is currently populating a listbox, to also populate a value in a textbox that is loaded once an item is selected from the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.