如何动态设置列表框的选定值 [英] How to set the selected value for a list box dynamically

查看:78
本文介绍了如何动态设置列表框的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个列表框,其中将基于下拉列表所选值显示一个列表框,如果我从第一个列表框选择一个项目并单击,则将其放置在第二个列表框和项目中并将其保存在数据库中,并将添加的项目从第一个列表框中删除,这可以正常工作.如果我选择另一个项目并仅将第二个列表框中的第一个项目保存,则正在保存.
下拉菜单中将显示以下内容
ABC
DEF
GHI
根据选择,我将把数据绑定到数据库中的第一个列表框,例如说我的第一个列表框如下
AB
CD
EF
现在,如果我选择AB并单击添加"按钮,则必须将其添加到第二个列表框和数据库中,以便应将其删除并在第二个中显示.
现在,如果我选择CD,第二个列表框将包含以下数据
AB
CD//默认情况下,我想按代码中的指定在单击按钮时选择此项目,以便仅插入CD
我需要的是将选定的值设置为下一个项目
这是我的代码

I am having 2 List boxes where one list box will be displayed based on the drop down list selected value and if i select an item from first list box and click on that item will be placed in to the second list box and item and will be saved in the database and the item that added will be removed from the first list box this works fine. If i select another item and saving only the first item in the second list box was saving.
Drop down will have the following
ABC
DEF
GHI
Based on the selection i will have the data binded to the first list box from the database say for example my First list box is as follows
AB
CD
EF
Now if i select AB and click on Add Button provided it has to be added to the second list box and the database so that it should be removed and will be shown in second.
Now if i select CD my second list box will have the data as follows
AB
CD // I would like to select this item by default on clicking button as per given in the code so that only CD should be inserted
What i need is here to set the selected value to the next item
Here is my code

<pre>protected void Add_Click(object sender, ImageClickEventArgs e)<br />
  {<br />
    AdminPaytypes.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;<br />
    CustomerPaytypes.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;<br />
    if (AdminPaytypes.SelectedIndex >= 0)<br />
    {<br />
        for (int i = 0; i < AdminPaytypes.Items.Count; i++)<br />
        {<br />
            if (AdminPaytypes.Items[i].Selected)<br />
            {<br />
                if (!array.Contains(AdminPaytypes.Items[i]))<br />
                {<br />
                    array.Add(AdminPaytypes.Items[i]);<br />
                }<br />
            }<br />
        }<br />
        for (int i = 0; i < array.Count; i++)<br />
        {<br />
            if (!CustomerPaytypes.Items.Contains(((ListItem)array[i])))<br />
            {<br />
                CustomerPaytypes.Items.Add(((ListItem)array[i]));<br />
  // AT this place i have to set the selected value to the array item i am getting<br />
            }<br />
            AdminPaytypes.Items.Remove(((ListItem)array[i]));<br />
        }<br />
    }<br />
// This J loop also running twice but i have to insert only one item<br />
    for (int j = 0; j < CustomerPaytypes.Items.Count; j++)<br />
    {<br />
        mlocal_strStoredProcName = "uspInsertCustomerPaytypes";<br />
        oSysAdminGrantPaytypes.FedTaxID = ddlEmployer.SelectedValue;<br />
        oSysAdminGrantPaytypes.PayFrequencyTypeID = CustomerPaytypes.SelectedItem.ToString();<br />
        oSysAdminGrantPaytypes.PayFrequencyDesc = CustomerPaytypes.SelectedValue;<br />
        oSysAdminGrantPaytypes.Insert(mlocal_strStoredProcName);<br />
        //CustomerPaytypes.Items.Clear();<br />
    }<br />
}</pre>

推荐答案

仅添加和删除两个列表之间的选定项.
Only add and remove the selected item between two lists.


这篇关于如何动态设置列表框的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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