RadListBox: How to disable moving of particular item

1.1k Views Asked by At

enter image description here

How to prevent a particular item from left to right using code on server side or by using javascript on client side.

Eg: If user want to move 'Argentina' from left to right,i want to prevent and show an alert message saying "It is not possible"

       RoleSelectedListBox = new RadListBox();
        RoleSelectedListBox.ID = "RoleSelectedListBox";
        RoleSelectedListBox.TabIndex = 1;
        //RoleSelectedListBox.CssClass = "RoleSelectedListBoxStyle";
        RoleSelectedListBox.SelectionMode = ListBoxSelectionMode.Multiple;
        RoleSelectedListBox.AllowTransfer = true;
        RoleSelectedListBox.TransferToID = "RoleAvailableListBox";
        RoleSelectedListBox.Skin = "FuzeCustom";
        RoleSelectedListBox.EnableEmbeddedSkins = false;
        RoleSelectedListBox.EnableDragAndDrop = true;
        RoleSelectedListBox.EnableMarkMatches = true;
        RoleSelectedListBox.Sort = RadListBoxSort.Ascending;
        RoleSelectedListBox.SortItems(); 
1

There are 1 best solutions below

3
On

Please try with the below code snippet.

Client Side

JS

<script type="text/javascript">
    function ClientTransferring(sender, args) {

        if (args.get_items().length > 0) {
            for (var i = 0; i < args.get_items().length; i++) {
                if (args.get_items()[i]._element.textContent == "Argentina") {
                    args.set_cancel(true);
                    alert("your message comes here");
                }
            }
        }
    }
</script>

ASPX

<telerik:RadListBox runat="server" ID="RadListBoxSource" Height="200px" Width="230px"
    AllowTransfer="true" TransferToID="RadListBoxDestination" OnClientTransferring="ClientTransferring">
    <Items>
        <telerik:RadListBoxItem Text="Argentina"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Australia"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Brazil"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Canada"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Chile"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="China"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Egypt"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="England"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="France"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Germany"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="India"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Indonesia"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Kenya"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Mexico"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="New Zealand"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="South Africa"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="USA"></telerik:RadListBoxItem>
    </Items>
</telerik:RadListBox>
<telerik:RadListBox runat="server" ID="RadListBoxDestination" Height="200px" Width="200px">
</telerik:RadListBox>

Server Side

ASPX

<telerik:RadListBox runat="server" ID="RadListBoxSource" Height="200px" Width="230px"
    AllowTransfer="true" TransferToID="RadListBoxDestination" AutoPostBackOnTransfer="true"
        OnTransferring="RadListBoxSource_Transferring">
    <Items>
        <telerik:RadListBoxItem Text="Argentina"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Australia"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Brazil"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Canada"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Chile"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="China"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Egypt"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="England"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="France"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Germany"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="India"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Indonesia"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Kenya"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="Mexico"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="New Zealand"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="South Africa"></telerik:RadListBoxItem>
        <telerik:RadListBoxItem Text="USA"></telerik:RadListBoxItem>
    </Items>
</telerik:RadListBox>
<telerik:RadListBox runat="server" ID="RadListBoxDestination" Height="200px" Width="200px">
</telerik:RadListBox>

ASPX.CS

protected void RadListBoxSource_Transferring(object sender, RadListBoxTransferringEventArgs e)
{
    if (e.Items.Count > 0)
    {
        foreach (RadListBoxItem item in e.Items)
        {
            if (item.Text == "Argentina")
            {
                e.Cancel = true;
                //show your message from here
            }
        }
    }
}