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).
|
|
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 )
<html> <head> <script language = "javascript"> function showText() { //This functions is used show selected value of listbox into textbox. var objselect; var txtval; objselect=document.forms['testform'].testselect; //Get the selected value from listbox txtval=objselect.options[objselect.selectedIndex].value; //Display it in textbox document.getElementById('Text1').value = txtval; } </script> </head> <body> <form> <% Dim con Dim rs Dim sql set con= server.CreateObject("ADODB.Connection") set rs= server.CreateObject("ADODB.Recordset") '''Open connection con.open "...." sql = "Select column1,column2 from table1" ''Open recordset set rs= con.execute(sql) %> <Select id="testSelect" onSelectedIndexChanged ="showText();"> <%While not rs.Eof%> <Option value="<%=rs(0)>"><%=rs(1)></Option> <%rs.movenext Wend> </Select> <input type="text" id= "Text1" value=""></input> </form> </body> </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..
|
|
|
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 )
<html> <body> Select a item: <select name=fruit> <% Dim objConn Set objConn=Server.CreateObject("ADODB.Connection") objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini" objConn.Open Dim sql sql="SELECT * FROM fruits" Dim objRS Set objRS=Server.CreateObject("ADODB.Recordset") objRS.Open sql,objConn Do while not objRS.EOF Response.Write "<option value'" & objRS("fnum") & "'>" Response.Write objRS("fruit") & "</option>" objRS.MoveNext Loop %> </select> <% objRS.Close Set objRS=Nothing objConn.Close Set objConn=Nothing %> <BR><input type=text name=price> </body> </html>
and our database table is like this..
Table
fnum fruit price
app Apple 300
mng Mango 1000
grp Grapes 500
|
|
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 )
Do while not objRS.EOF Response.Write "<option value'" & objRS("price") & "'>" Response.Write objRS("fruit") & "</option>" objRS.MoveNext 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
|
|
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??
|
|
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 )
Do while not objRS.EOF Response.Write "<option value'" & objRS("fnum") & "*" & objRS("price") &"'>" Response.Write objRS("fruit") & "</option>" objRS.MoveNext 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 )
var sOption = document.FormName.fruit.options[document.FormName.fruit.selectedIndex].value; var sOptionArray = sOption.split("*"); document.FormName.price.value = sOptionArray[1];
vbscript :
Code: ( text )
If Instr(Request("fruit"), "*") > 0 Then fnum = Split(Request("fruit"), "*")(0) price= Split(Request("fruit"), "*")(1) End If
Let me know how you get on.
Dr B
|
|
May 13th, 2008 08:58 AM
# 7
|
Re: Filling TextBox Using Change in ListBox(Database Related)
HI..
Code: ( text )
<html> <head> <script language = "javascript"> function showText() { if ((document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value)==300) { document.TestForm.Text1.value=document.TestForm.fr uit.options[document.TestForm.fruit.selectedIndex].text; } else { document.TestForm.Text2.value=document.TestForm.fr uit.options[document.TestForm.fruit.selectedIndex].text; } } function show() { var sOption = document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value; var sOptionArray = sOption.split("*"); document.TestForm.price.value = sOptionArray[1]; } </script> </head> <body> <form method=post name=TestForm action="d5.asp"> Select a item: <select name=fruit onchange="show();"> <option value='1*300'>Apple</option> <option value='2*300'>Mango</option> <option value='3*500'>Grapes</option> </select><BR> <input type=text name=Text1 id=Text1><BR> <input type=text name=Text2 id=Text2> </body> </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 !
|
|
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
|
|
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!!
|
|
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.
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
|