Thursday, October 07, 2010

How to move items between two ListBoxes

Demo:

<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script runat="server">
        protected void MoveS(object sender, EventArgs e)
        {
            this.lstSource.ClearSelection();
            if (this.lstDestination.SelectedIndex > -1)
            {
                string s = this.lstDestination.SelectedValue;
                this.lstSource.Items.Add(this.lstDestination.SelectedItem);
                this.lstDestination.Items.Remove(this.lstDestination.SelectedItem);
                this.lstSource.SelectedValue = s;
            }
        }
        protected void MoveD(object sender, EventArgs e)
        {
            this.lstDestination.ClearSelection();
            if (this.lstSource.SelectedIndex > -1)
            {
                string s = this.lstSource.SelectedValue;
                this.lstDestination.Items.Add(this.lstSource.SelectedItem);
                this.lstSource.Items.Remove(this.lstSource.SelectedItem);
                this.lstDestination.SelectedValue = s;
            }
        }
        protected void Edit(object sender, EventArgs e)
        {
            this.hdnSelection.Value = string.Empty;
            this.txtEdit.Text = string.Empty;
            if (this.lstSource.SelectedIndex > -1)
            {
                this.txtEdit.Text = this.lstSource.SelectedValue;
                this.hdnSelection.Value = "S";
                this.lstSource.Items.Remove(this.lstSource.SelectedItem);
            }
            else if (this.lstDestination.SelectedIndex > -1)
            {
                this.txtEdit.Text = this.lstDestination.SelectedValue;
                this.hdnSelection.Value = "D";
                this.lstDestination.Items.Remove(this.lstDestination.SelectedItem);
            }
            if (!string.IsNullOrEmpty(this.txtEdit.Text))
            {
                this.pnlEdit.Visible = true;
                this.pnlItems.Enabled = false;
            }
        }
        protected void Save(object sender, EventArgs e)
        {
            if (this.hdnSelection.Value.Equals("D"))
                this.lstDestination.Items.Add(new ListItem(this.txtEdit.Text));
            else if (this.hdnSelection.Value.Equals("S"))
                this.lstSource.Items.Add(new ListItem(this.txtEdit.Text));
            this.txtEdit.Text = string.Empty;
            this.hdnSelection.Value = string.Empty;
            this.pnlEdit.Visible = false;
            this.pnlItems.Enabled = true;
        }
        protected void ClearDestination(object sender, EventArgs e)
        {
            this.lstDestination.ClearSelection();
        }
        protected void ClearSource(object sender, EventArgs e)
        {
            this.lstSource.ClearSelection();
        }
    </script>
</head>
 
<body>
    <form runat="server" id="form1">
        <asp:ScriptManager runat="server" ID="pageScriptManager" />
        <asp:UpdatePanel runat="server" ID="upnlEdit">
            <ContentTemplate>
                <asp:Panel runat="server" ID="pnlItems">
                    <asp:ListBox runat="server" ID="lstSource" 
                        OnSelectedIndexChanged="ClearDestination" AutoPostBack="true">
                        <asp:ListItem>Car</asp:ListItem>
                        <asp:ListItem>Van</asp:ListItem>
                        <asp:ListItem>Truck</asp:ListItem>
                        <asp:ListItem>Bike</asp:ListItem>
                        <asp:ListItem>Cycle</asp:ListItem>
                    </asp:ListBox>
                    <asp:ListBox runat="server" ID="lstDestination" 
                        OnSelectedIndexChanged="ClearSource" AutoPostBack="true" />
                </asp:Panel>
                <hr />
                <asp:Button runat="server" ID="btnMoveD" OnClick="MoveD" Text=">>" />
                <asp:Button runat="server" ID="btnMoveS" OnClick="MoveS" Text="<<" />
                <asp:Button runat="server" ID="btnEdit" Text="Edit" OnClick="Edit" />
                <hr />
                <asp:Panel runat="server" ID="pnlEdit" Visible="false">
                    <asp:TextBox runat="server" ID="txtEdit"></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ID="rvalEdit" 
                        ControlToValidate="txtEdit" Display="Dynamic" ErrorMessage="*" ValidationGroup="Edit" />
                    <asp:HiddenField runat="server" ID="hdnSelection" />
                    <asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save"  ValidationGroup="Edit" />
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...