Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,
I am adding items to List-box using java-script;
Now , on Button click server side event, when I try to remove selected items, I get item count 0;

I want to remove items from server side click event only.

This is what I have on Keyup event of text box.
<asp:TextBox ID="txtPONo" runat="server"  onKeyUp="inputKeyUp(event)"></asp:TextBox>
<asp:Button ID="btnAddToList" runat="server" OnClick="btnRemoveFromList_Click" Text="Remove PO" />

<script type="text/javascript">
    function inputKeyUp(e) {
        e.which = e.which || e.keyCode;
        if (e.which == 13) {
            var varFromBox = document.all('lstPONo');
            var ListBox = document.getElementById('ctl00_MainContentPlaceHolder_lstPONo');
            var TextBox = document.getElementById('ctl00_MainContentPlaceHolder_txtPONo');

            var myOption = new Option();
            myOption.text = document.getElementById('ctl00_MainContentPlaceHolder_txtPONo').value; //Textbox's value
            myOption.value = document.getElementById('ctl00_MainContentPlaceHolder_txtPONo').value; //Textbox's value
            ListBox.add(myOption);
            document.getElementById('ctl00_MainContentPlaceHolder_txtPONo').value = '';
        }
    }
</script>


And my button click event on server side :
protected void btnRemoveFromList_Click(object sender, EventArgs e)
        {
            try
            {
                while (lstPONo.Items.Count > 0)
                {
                    lstPONo.Items.Remove(lstPONo.SelectedItem);
                }
            }
            catch (Exception ex)
            {
                XITingExceptionProcessor.ProcessException(this, ex);
            }
        }


I cant find out, where I am going wrong.
Please help,

Thanks,
Lok..
Posted
Comments
ZurdoDev 21-Mar-12 7:57am    
Silly question, but if you are adding them client side why don't you also remove them client side?
solutions@ashish 21-Mar-12 9:07am    
Agree with ryanb31,

If You create any thing using javascript so why you want to remove them using server side

You are adding items on the HTML generated on the client browser so server has no idea of what is being done in the client browser.So you always get the count of items 0.
 
Share this answer
 
Comments
Lokesh Zende 22-Mar-12 0:33am    
Thanks for this info, Sastry_kunapuli...
As pointed out by Sastry_kunapuli, you are adding the items on client side, the server has no idea about them, so you are getting count = 0 at server side.

If you must do it this way, then here is a way:

1. Add a hidden input field in the form.

HTML
<input type="hidden" name="added_to_list" id="added_to_list" value=""></input>


2. Before submitting the form to the server, populate the hidden input field with the items added to the list box at client side. Use some delimiter between entries.

3. On the server, read the hidden input field to retrieve the added items, separating on the delimiter.
 
Share this answer
 
Comments
Lokesh Zende 22-Mar-12 0:34am    
Yes Shreekar,
I did the same way. :)
Hi,
You can remove selected Items from list at server side on click of button.
Listbox remove selected items.
Find list of selected list items and remove them from list box.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900