filling a textbox corresponding to a value selected in listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aashishn86
    New Member
    • Mar 2009
    • 51

    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.......
  • jenkinsloveschicken
    New Member
    • Dec 2006
    • 56

    #2
    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):

    Code:
    <%
    
    myDynamicallyPopulatedValue = "Test"
    
    Response.write "<select name='mySelect' id='mySelect' onchange='updateUI(this.value);' value=''>"
    Response.write "<option value='" & myDynamicallyPopulatedValue & "' >" &  myDynamicallyPopulatedValue & "</option>"
    Response.write "</select>"
    Response.write "<input type='text' name='myTargetBox' id='myTargetBox' value='' />"
    
    %>
    Now for the j/s to make it work:

    Code:
    <script type="text/javascript">
    function updateUI(userValue)
    {
    var userSelection = userValue;
    var targetBox = document.getElementById("myTargetBox");
    targetBox.value = userSelection;
    }
    </script>
    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

    Comment

    • aashishn86
      New Member
      • Mar 2009
      • 51

      #3
      Code:
      <%@ LANGUAGE="VBScript" %> 
      <html>
      <body>
      
      <script type="text/javascript"> 
      function updateUI(userValue) 
      { 
      var userSelection = userValue; 
      var targetBox = document.getElementById("myTargetBox"); 
      TargetBox.value = userSelection; 
      } 
      </script> 
      
        
      
      <%
        strcon = "provider=sqloledb;data source=1.234.5.678,1234;initial catalog = abcde; User Id=abcdef ;password=abcde"
        Set objcon = Server.CreateObject("ADODB.Connection")
        objcon.ConnectionString = strcon
        objcon.Open
      
        strsql = "select cname from test3"
        
        Set objrset = Server.CreateObject("ADODB.Recordset")
            objrset.Open strsql,strcon,2,3,1 %>
            
      
      <select name = "select one" onchange= 'updateUI(this.value)' >
      <% while not objrset.eof %>
      <option value = "<%=objrset("cname")%>"><%=objrset("cname")%></option>
      <%objrset.movenext
        wend %> 
      </select> 
      
      
      <input type = "text" size="20" name="mytargetbox">
      
      </body>
      </html>
      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...

      Comment

      • jenkinsloveschicken
        New Member
        • Dec 2006
        • 56

        #4
        Ok, just so you are aware, javascript is case sensitive. Change your function to this:

        Code:
        <script type="text/javascript"> 
        function updateUI(userValue) 
        { 
        //Remove the alert line once you have confirmed you are getting the correct value.
        alert (userValue);
        var userSelection = userValue; 
        var targetBox = document.getElementById("mytargetbox"); 
        targetBox.value = userSelection; 
        } 
        </script>
        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

        Comment

        Working...