Get listbox values added via javascript on server side via hidden field....
See more: ASP.NET
Get listbox values added via javascript on server side via hidden field....

Recommended Answers

All 4 Replies

Your main thing to do is get the hidden field's name correct in javascript, which you can do via view source of the page in IE or another browser. I would set the hidden field's value either on a button press which fires the function that does this or through a keypress event in javascript.

Hope that helps a little,
Larry

Please provide a complete code of getting list box values added client side to server side...

Can you post your code you are using to populate the listbox? It seems you are adding items dynamically on client and I can add code to that to fill the hidden field with the new entries.

Thanks,
Larry

Ok, here is a mock up for you, it fires when the listbox has a different selection made, I do not know what code you are using to populate the listbox or what code you are using to add entries, you can place the code that is in the .change event into your jquery that adds new entries to the listbox.

<input type='hidden' name='ctl00$ContentPlaceHolder1$txtTest' id='ctl00_ContentPlaceHolder1_txtTest' class='txt'></input>
You will be able to pick up the listbox items by looking at the txtTest.value in your code behind
<select id='dropdown' class='dd'>
<OPTION VALUE="Cape Fear">Cape Fear
<OPTION VALUE="The Good, the Bad and the Ugly">The Good, the Bad and the Ugly
<OPTION VALUE="The Omen">The Omen
<OPTION VALUE="The Godfather">The Godfather
<OPTION VALUE="Forrest Gump">Forrest Gump
</select>
<script>
$('.dd').change(function () {
    //this fires when the listbox has a different selection made
    //you should put this where a new entry is created for the listbox
    var vals='|';//initialize the variable
    $('.dd').children().each(function() {
        vals=vals + $(this).val() + '|';//set the variable to the dropdownlist value
        }
});

//alert(vals);
    //the following line sets the class "txt" which is the textbox to the dropdownlist entries
$('.txt').val(vals);
});
</script>

You can change the <input type='hidden' to <input type='text' and then you can see the values filling in for testing. We will need to finetune it depending on what you are making and the variables you are using. Just remember to set the txtTest textbox cssClass to "txt" and set the listbox cssClass to "dd" that way you can reference them from jquery easier.

Let me know if you need more help,
Larry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.