Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Filling TextBox Using Change in ListBox(Database Related)

Question posted by: Vini171285 (Member) on May 11th, 2008 05:22 AM
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 column of the table in database..
What code should be written for this??
Thanx..
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
shweta123's Avatar
shweta123
Expert
616 Posts
May 11th, 2008
03:47 PM
#2

Re: Filling TextBox Using Change in ListBox(Database Related)
Hi,

You can do it this way:
Code: ( text )
  1. <html>
  2.   <head>
  3.    <script language = "javascript">
  4.      function showText()
  5.       {
  6.         //This functions is used show selected value of listbox into textbox.
  7.          var objselect;
  8.          var txtval;
  9.         objselect=document.forms['testform'].testselect;
  10.  
  11.        //Get the selected value from listbox
  12.         txtval=objselect.options[objselect.selectedIndex].value;
  13.  
  14.        //Display it in textbox
  15.         document.getElementById('Text1').value = txtval;
  16.       }
  17.  
  18.    </script>
  19.   </head>
  20.    <body>
  21.    <form>
  22.     <%
  23.       Dim con
  24.       Dim rs
  25.       Dim sql
  26.  
  27.       set con= server.CreateObject("ADODB.Connection")
  28.       set rs= server.CreateObject("ADODB.Recordset")
  29.  
  30.         '''Open connection
  31.       con.open "...." 
  32.       sql = "Select column1,column2 from table1"
  33.  
  34.       ''Open recordset
  35.       set rs= con.execute(sql)
  36.  
  37.      %> 
  38.    <Select id="testSelect" onSelectedIndexChanged ="showText();">
  39.   <%While not rs.Eof%>
  40.    <Option value="<%=rs(0)>"><%=rs(1)></Option>
  41.   <%rs.movenext
  42.    Wend>
  43.   </Select>
  44.   <input type="text" id= "Text1" value=""></input>
  45.    </form>
  46.    </body>
  47.     </html>



Quote:
Originally Posted by Vini171285
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 column of the table in database..
What code should be written for this??
Thanx..

Reply
Vini171285's Avatar
Vini171285
Member
60 Posts
May 12th, 2008
11:01 AM
#3

Re: Filling TextBox Using Change in ListBox(Database Related)
Hi
This code is not working...
Actually i am having a list box and after u change the value of list box ,the price corresponding to the selected index should come in textbox..
This is my code...

Code: ( text )
  1. <html>
  2. <body>
  3. Select a item:
  4. <select name=fruit>
  5. <%
  6.     Dim objConn
  7.     Set objConn=Server.CreateObject("ADODB.Connection")
  8.     objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
  9.     objConn.Open
  10.  
  11.     Dim sql
  12.     sql="SELECT * FROM fruits"
  13.  
  14.     Dim objRS
  15.     Set objRS=Server.CreateObject("ADODB.Recordset")
  16.     objRS.Open sql,objConn
  17.  
  18.     Do while not objRS.EOF
  19.         Response.Write "<option value'" & objRS("fnum") & "'>"
  20.         Response.Write objRS("fruit") & "</option>"
  21.         objRS.MoveNext
  22.     Loop
  23. %>
  24. </select>
  25. <%
  26.     objRS.Close
  27.     Set objRS=Nothing
  28.  
  29.     objConn.Close
  30.     Set objConn=Nothing
  31. %>
  32. <BR><input type=text name=price>
  33. </body>
  34. </html>


and our database table is like this..

Table
fnum fruit price
app Apple 300
mng Mango 1000
grp Grapes 500

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
May 12th, 2008
01:48 PM
#4

Re: Filling TextBox Using Change in ListBox(Database Related)
You can store the price in the value property of your Drop Down options. Then you can use Shweta's bit of javascript to populate the textbox.

Like so:
Code: ( text )
  1. Do while not objRS.EOF
  2.         Response.Write "<option value'" & objRS("price") & "'>"
  3.         Response.Write objRS("fruit") & "</option>"
  4.         objRS.MoveNext
  5. Loop

Will this solution work for you? It may be that you need to store fnum in the value field of your Drop Down List in which case we'll need to be a bit cleverer.

Let me know how this works for you.

Dr B

Reply
Vini171285's Avatar
Vini171285
Member
60 Posts
May 13th, 2008
04:49 AM
#5

Re: Filling TextBox Using Change in ListBox(Database Related)
Hi!
The code we tried,its working, but it is not the solution to our problem..
If we store price as value in option,then while storing it in database we cant store the price because it can be same for many options..
and it will create problems while retrieving data..
Is there any other solution??

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
May 13th, 2008
07:34 AM
#6

Re: Filling TextBox Using Change in ListBox(Database Related)
In that case you need some way to store both the fnum AND the price when you make your initial query of the database. There are a few ways you can do this but probably the simplest is to store both of them in the value field of the Drop Down but separate them with a special character. Take a look at this example:
Code: ( text )
  1. Do while not objRS.EOF
  2.    Response.Write "<option value'" & objRS("fnum") & "*" & objRS("price") &"'>"
  3.    Response.Write objRS("fruit") & "</option>"
  4.    objRS.MoveNext
  5. Loop

You can see that both fnum and price are stored as the value and are separated by the * character. Now you need to retrieve each of the values both in your javascript function when you populate the textbox and also in your asp code when you want to use the fnum value. Here is an example of each:

javascript:
Code: ( text )
  1. var sOption = document.FormName.fruit.options[document.FormName.fruit.selectedIndex].value;
  2.  var sOptionArray = sOption.split("*");
  3.  document.FormName.price.value = sOptionArray[1];


vbscript:
Code: ( text )
  1. If Instr(Request("fruit"), "*") > 0 Then
  2.       fnum = Split(Request("fruit"), "*")(0)
  3.       price= Split(Request("fruit"), "*")(1)
  4. End If

Let me know how you get on.

Dr B

Reply
Vini171285's Avatar
Vini171285
Member
60 Posts
May 13th, 2008
08:58 AM
#7

Re: Filling TextBox Using Change in ListBox(Database Related)
HI..

Code: ( text )
  1. <html>
  2. <head>
  3.    <script language = "javascript">
  4.      function showText()
  5.       {
  6.        if ((document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value)==300)
  7.         {
  8.         document.TestForm.Text1.value=document.TestForm.fr  uit.options[document.TestForm.fruit.selectedIndex].text;
  9.         }
  10.         else
  11.         {
  12.         document.TestForm.Text2.value=document.TestForm.fr  uit.options[document.TestForm.fruit.selectedIndex].text;
  13.         }
  14.       }
  15.       function show()
  16.       {
  17.       var sOption = document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value;
  18.       var sOptionArray = sOption.split("*");
  19.       document.TestForm.price.value = sOptionArray[1];
  20.        }
  21.  
  22.  
  23.    </script>
  24.   </head>
  25. <body>
  26. <form method=post name=TestForm action="d5.asp">
  27. Select a item:
  28. <select name=fruit onchange="show();">
  29.     <option value='1*300'>Apple</option>
  30.     <option value='2*300'>Mango</option>
  31.     <option value='3*500'>Grapes</option>
  32. </select><BR>
  33.  
  34. <input type=text name=Text1 id=Text1><BR>
  35. <input type=text name=Text2 id=Text2>
  36. </body>
  37. </html>


I went through the code that you provided but,I am not getting the correct position to place the vbscript code..
Just tell me where exactly to place the vb-script part in this code

Thanx !

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
May 13th, 2008
10:17 AM
#8

Re: Filling TextBox Using Change in ListBox(Database Related)
You don't need to the vbscript on this page but you will need it on the page that this form loads when it submits in order to get the values that are passed through the request object.

The code you've printed above should work just fine.

Has it worked okay for you?

Dr B

Reply
Vini171285's Avatar
Vini171285
Member
60 Posts
May 13th, 2008
10:27 AM
#9

Re: Filling TextBox Using Change in ListBox(Database Related)
Hi Dr B,

Thanx for the solution ...

The code is working well...
Thanx!!

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
May 13th, 2008
10:31 AM
#10

Re: Filling TextBox Using Change in ListBox(Database Related)
No problem Vini, glad it's working for you.

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,967 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top ASP Forum Contributors