从列表框中删除项目 [英] Remove Items from ListBox

查看:92
本文介绍了从列表框中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
我正在使用java-script将项目添加到列表框;
现在,在Button单击服务器端事件时,当我尝试删除选定的项目时,我得到的项目计数为0;

我只想从服务器端单击事件中删除项目.

这是我对文本框的Keyup事件的了解.

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);
            }
        }



我找不到我要去哪里了.
请帮忙,

谢谢,
Lok ..



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

Thanks,
Lok..

推荐答案

您正在在客户端浏览器上生成的HTML上添加项目,因此服务器不知道客户端浏览器正在做什么.获得项目计数0.
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.


正如Sastry_kunapuli所指出的,您正在客户端添加项目,服务器对此一无所知,因此在服务器上获得计数= 0侧面.

如果您必须以这种方式进行操作,则可以使用以下方法:

1.在表单中添加一个隐藏的输入字段.

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.

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



2.在将表单提交到服务器之前,请在隐藏的输入字段中填充添加到客户端列表框中的项目.在条目之间使用一些定界符.

3.在服务器上,阅读隐藏的输入字段,以检索添加的项目,并在定界符处进行分隔.



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.


Hi,
单击按钮,可以从服务器端的列表中删除选定的项目.
列表框删除选定的项目.
查找所选列表项的列表并将其从列表框中删除.
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.


这篇关于从列表框中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆