Moving item between the listboxes on server side

(ASP.NET)


Hi,
In this article i placed the two listboxes, on the button click event i move the item between two listboxes on server side.

.aspx page
<div>
 <asp:ListBox ID="lstbx_source1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem>Item 4</asp:ListItem>
        </asp:ListBox>
    
         <asp:ListBox ID="lstbx_source2" runat="server">
              <asp:ListItem>Item 5</asp:ListItem>
             <asp:ListItem>Item 6</asp:ListItem>
             <asp:ListItem>Item 7</asp:ListItem>
             <asp:ListItem>Item 8</asp:ListItem>
         </asp:ListBox>
        <br />
        <asp:Button  ID="btn_lst1"  OnClick="btn_lst1_Click" runat="server" Text="Move to List 2" /> &nbsp;&nbsp;
        <asp:Button ID="btn_lst2" OnClick="btn_lst2_Click" runat="server" Text="Move to List 1" />
       
        </div>

Code behind:-

    protected void btn_lst1_Click(object sender, EventArgs e)
        {
            try
            {
                string lstValue = lstbx_source1.SelectedItem.Value;
                string lstText = lstbx_source1.SelectedItem.Text;

                ListItem item = new ListItem();
                item.Text = lstText;
                item.Value = lstValue;

                lstbx_source2.Items.Add(item);

                lstbx_source1.Items.Remove(item);
            }
            catch (Exception)
            { }
        }

        protected void btn_lst2_Click(object sender, EventArgs e)
        {
            try
            {
                string lstValue = lstbx_source2.SelectedItem.Value;
                string lstText = lstbx_source2.SelectedItem.Text;

                ListItem item = new ListItem();
                item.Text = lstText;
                item.Value = lstValue;

                lstbx_source1.Items.Add(item);

                lstbx_source2.Items.Remove(item);


            }
            catch (Exception)
            { }
        }



Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/28/2013
6/28/2013
Download PDF

Comments