none
Add/Remove items in listbox PERMANENTLY

    Question

  • Hi, I would like to know how to add/remove items from listbox PERMANENTLY. Sad to say, all I have found are adding/removing items temporarily.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
        <script language="javascript" type="text/javascript" >
            function addOption(selectbox,text,value )
            {
    	        var optn = document.createElement("OPTION");
    	        optn.text = text;
    	        optn.value = value;
    	        selectbox.options.add(optn);
            }
    
            function addOption_list(selectbox)
            {
                addOption(document.drop_list.SubCat, "One","One");
                addOption(document.drop_list.SubCat, "Two","Two");
                addOption(document.drop_list.SubCat, "Three","Three");
                addOption(document.drop_list.SubCat, "Four","Four");
                addOption(document.drop_list.SubCat, "Five","Five");
                addOption(document.drop_list.SubCat, "Six","Six");
            }
    
            function removeOptions(selectbox)
            {
    	        var i;
    	        for(i=selectbox.options.length-1;i>=0;i--)
    	        {
    	            if(selectbox.options[i].selected)
    	            selectbox.remove(i);
    	        }
            }
        </script>
    </head>
    
    <body onload="addOption_list()";>
    <form name="drop_list" action="default.aspx" method="post" >
        <select id="SubCat" name="SubCat" MULTIPLE size="6" width="10"></select>
        <input type="button" onclick="removeOptions(SubCat)"; value='Remove Selected' />
        <input type="button" onclick="addOption_list()"; value='Add All' /> 
    </form>
    </body>
    
    </html>
    

    Please advise me on the problem.
    Thursday, January 14, 2010 5:37 AM

Answers

  • It appears that you have the list hard-coded to a set of values in the addOption_list function. So every time the page is loaded, you repopulate the list with your hard-coded values.

    If you want the list to be changable, you need to not hard-code the values into your code.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Roahn Luo Monday, January 18, 2010 9:52 AM
    Thursday, January 14, 2010 10:21 PM
  • You have lots of options, depending on what the data is and what it is used for.

    You could put the values into a database. Then your code would need to read them from the database to populate the list and save any changes to the database.

    You could put the values into cookies if they are only useful for a particular user. Then you could populate the list from cookies and save any changes to the cookies.

    You may want to consider posting a question with more specific information on the type of data you want to save to the ASP.NET forums:

    http://forums.asp.net/

    They can then provide code examples based on the technique you select to use.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Roahn Luo Monday, January 18, 2010 9:52 AM
    Friday, January 15, 2010 1:26 AM

All replies

  • It appears that you have the list hard-coded to a set of values in the addOption_list function. So every time the page is loaded, you repopulate the list with your hard-coded values.

    If you want the list to be changable, you need to not hard-code the values into your code.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Roahn Luo Monday, January 18, 2010 9:52 AM
    Thursday, January 14, 2010 10:21 PM
  • Okay, so how should I change it to be not hard-coded anymore?
    Friday, January 15, 2010 1:13 AM
  • You have lots of options, depending on what the data is and what it is used for.

    You could put the values into a database. Then your code would need to read them from the database to populate the list and save any changes to the database.

    You could put the values into cookies if they are only useful for a particular user. Then you could populate the list from cookies and save any changes to the cookies.

    You may want to consider posting a question with more specific information on the type of data you want to save to the ASP.NET forums:

    http://forums.asp.net/

    They can then provide code examples based on the technique you select to use.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Roahn Luo Monday, January 18, 2010 9:52 AM
    Friday, January 15, 2010 1:26 AM