简体   繁体   中英

Asp.net listbox used client side

I have two asp.net listbox control in my page lbox1 and lbox2 lbox1 is filled in code behind . Now user can select items on lbox1 and by clicking on a button the selected items goes in lbox2. I do this using javascript becouse I don't want postback on each click.

this is the javascript function :

function Updatelist() {
            var sel = document.getElementById("lbox1");
            var listLength = sel.options.length;
            for (var i = 0; i < listLength; i++) {
                if (sel.options[i].selected)
                    document.getElementById("lbox2").add(new Option(sel.options[i].value));
            } 
        }

Now I need to send on server side the content of lbox2 using another button. I think that using a simple asp button with a onserverclick event not work becouse in server side lbox2 is never filled!

How can I do ?

You'll need to add a button and JS function that copies the values from the lbox1 control into an <asp:HiddenField> control. Once you post back, the selected values will be available in the hidden control's Value property.

To avoid postback use html dropdown... and for your code ans will be below:-

function Updatelist() { var sel = document.getElementById("lbox1");

        var listLength = sel.options.length;

        var opt = document.createElement('option');

        document.getElementById("lbox2").options.add(opt);            
         for (var i = 0; i < listLength; i++) {
             if (sel.options[i].selected) {
                 document.getElementById("lbox2").options.add(opt);
                 opt.text = sel.options[i].value;
                 opt.value = sel.options[i].value;
             }
        } 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM