Link to home
Start Free TrialLog in
Avatar of ussharma
ussharma

asked on

Add items to a drop down control at run time

Hi all of you,

I am trying to add items to a drop down control at run time. Items will be added to it on clicking a button. I have associated a VBScript function to the button on it onClick event.
But unfortunately the add statement to the drop down control is not working. Please help.
My GO button declaration is like this
<.........GoButton........................ onClick = "loadAttributes()">
 
lstAttribute is the drop down list which should get loaded with some items.
 
SCRIPT Code section:

<SCRIPT language="VBScript">
sub loadAttributes()
 
lstAttribute.add "ABC",1                         (not working ERROR : Type Mismatch. I have also tried lstAttribute.addItem  “ABC”,1  . it is also not           working )
 
end sub
</SCRIPT>
 
please help out with the example.
 
Avatar of monvelasquez
monvelasquez

if you wouldn't mind, i would rather do this in javascript. here's a sample..

--[CODE]----------------------------------------------------
<HTML>
<head>
<script language="javascript">
   function loadAttributes() {
     var newOp = new Option("Option 2",2);
     document.frm.lstAttribute.add(newOp);
   }
</script>

</head>
<body>

<form name="frm">
<select name="lstAttribute" size="10">
   <option value="1">Option 1
</select>

<input type="button" name="add" value="Add" onclick="loadAttributes()">

</form>
</bodY>
</HTML>
--[/CODE]---------------------------------------------------
Usha,
R u going to do that in the server side or the client side..
If the values are fetched from DB, then I have another option.
Please clear this
ASKER CERTIFIED SOLUTION
Avatar of vijay7248
vijay7248

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ussharma

ASKER

i need to access the values from the database in Oracle using ADODB connection. but the loading of the drop down list have to be done using VBScript. i have done the same in javascript. dont know why its not working
in VBScript (neither add nor addItem).  
Hi Sharma,

I read through your doubt and was trying to solve the problem in VBScript. I could not find any solution or reference for the same. I have developed a javascript version of the same code.. You can also include your VBScript functions in the same. i.e your additem code alone would refer to Javascript and other functions can refer to your VBScript Code

#Code Listing#

<html>
      <head>
            <title>Javascript List Populate</title>

            <style>
                  BODY { font-family: Verdana, Arial, Helvetia; font-size: 11px; }
            </style>
            
            <script language="javascript">
                  
                  function addItem()
                  {
                        var frm;
                        frm = document.listDemo;
                        if (frm.txtText.value != "" && frm.txtValue.value != "")
                        {
                              var newOption = new Option(frm.txtText.value, frm.txtValue.value);
                              frm.lstAttribute.options[frm.lstAttribute.length] = newOption;
                              frm.lstAttribute.value = frm.txtValue.value;
                        }
                        else
                        {
                              alert("Please enter the Text to be displayed and value");
                        }
                  }

            </script>

            <script language = "vbscript">
                                      'Write the other VBScript code here
            </script>

      </head>
      <body>
            <h3>List Box Demo :: Populate a new item </h3>
            <form name="listDemo">
                  <select name="lstAttribute">
                        <option value="1">One</option>
                        <option value="2">Two</option>
                  </select>
                  <br /><br /><b>Add a new item</b><br />
                  Item Text : <input type="text" name="txtText" size="30" /> Item Value <input type="text" name="txtValue" size="30" />
                  <br /><br />
                  <input type="button" name="btnAdd" value="Add the new item" onclick="javascript:addItem();" />
            </form>
      </body>
</html>
If your loading populating the listbox with DB values...
Then here's some ASP code. Try this simple and compact method

Let's say ur query is 'qry' and u r storing the result set in 'rs'
Assuming Conn as your Connection Object
qry="select rec_id,first_name from tab1"
set rs=Conn.Execute(qry)  'You can also use Conn.Open here
Then the following code gives you a listbox

Response.Write "<select name='Listbox1'>" & _
   "<option selected value='All'>--All--</option>"
if not rs.eof then
   Response.Write("<option value='")
   Response.Write rs.getstring(,,"'>", _
             "</option><option value='", "-null-")
   Response.Write("'></option>")
end if
Response.write "</select>"

'The above code stores rec_id in the value of OPTIONS and first_name is displayed as the TEXT..
Try this it is very simple

For further reference regarding this code pls refer
http://www.4guysfromrolla.com/webtech/102600-1.shtml

'NOTE : This is a server-side code.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: vijay7248 {http:#9737458}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

vnvk
EE Cleanup Volunteer