Search code examples
c#asp.netradgridradlistbox

Get all the transferred RadListBox items on Label & bind a RadGrid using RadListBox_items, ID


1.) I am using below code to transfer items from one RadListBox to another.

HTML code:

A<telerik:RadListBox ID="RadListBox1" runat="server" Width="250px" Height="200px" 
 TransferMode="Move" AllowTransfer="True" TransferToID="rlbGI_BU" SelectionMode="Multiple"
 OnTransferred="RadListBox1_Transferred" 
 AutoPostBackOnTransfer="true">
</telerik:RadListBox>
B<telerik:RadListBox ID="RadListBox2" runat="server" Width="250px" Height="200px"
 EnableEmbeddedSkins="False" Skin="MetroRed" ImagesPath="../App_Themes/MetroRed/ListBox"
 OnInserted="RadListBox2_Inserted" SelectionMode="Multiple"
 OnDeleted="RadListBox2_Deleted" AutoPostBackOnDelete="true">
</telerik:RadListBox>
<br />
<asp:Label runat="server" ID="lblDeletedList2"></asp:Label> &nbsp; &nbsp;
<asp:Label runat="server" ID="lblInsertedList2"></asp:Label> &nbsp; &nbsp;
<asp:Label runat="server" ID="lblTransdList1"></asp:Label> &nbsp; &nbsp;

C# code:

private string _SPID = null;
private DataTable _dtSPBU = null;

protected void Page_Load(object sender, EventArgs e)
{
    _SPID = Request.QueryString["SPID"];
    if (!string.IsNullOrEmpty(_SPID))
    {
        _dtSPBU = SDM.SPBU.GetSPBUBySPBUfoSPID(_SPID);
    }
}

protected void bind_RadListBox1()
{
    RadListBox1.DataTextField = "BUName";
    RadListBox1.DataValueField = "SPBUfoBUID";
    RadListBox1.DataSource = _dtSPBU;
    RadListBox1.DataBind();
}

protected void RadListBox2_Inserted(object sender, RadListBoxEventArgs e)
{
    ArrayList myTest = new ArrayList();
    //StringBuilder s = new StringBuilder();
    for (int i = 0; i < e.Items.Count; i++)
    {
        myTest.Add(rlbGI_BU.Items[i].Text);

        foreach (string x in myTest)
        {
                lblInsertedList2.Text += x + ", " + "&nbsp;";
        }
    } 
}

protected void RadListBox2_Deleted(object sender, RadListBoxEventArgs e)
{
    lblDeletedList2.Text += e.Items.Count.ToString() + " items are deleted";
}

protected void RadListBox1_Transferred(object sender, RadListBoxTransferredEventArgs e)
{       
    lblTransdList1.Text += e.Items.Count.ToString() + " items are transferred";
}

I want to get the transferred Items from RadListBox1 to RadListBox2 inside a Label control (say lblInsertedList2 Label) because later I have to bind a RadGrid based on the Items that are inside RadListBox2.
For this, I created RadListBox2_Inserted event, inside which I am trying to get the items of RadListBox2 in Label.
Now, When I transfer more than 1 items of RadListBox1 to RadListBox2 (using ctrl button), then the 1st selected item is repeated.

Eample: RadListBox1 has "a","b","c" items. when I transfer all "a","b","c" together from RadListBox1 to RadListBox2 (using ctrl button)

I see the below output: a a b a b c

Correct output should be: a b c

Please check below snapshot for above issue: enter image description here Again, RadListBox1 has "a","b","c" items when I transfer items 1 by 1 from RadListBox1 to RadListBox2

Eample: when I only transfer a, I see below output: a (1st time) when I only transfer b, I see below output: a, a (2nd time) when I only transfer c, I see below output: a, a, a (3rd time)

Please check below snapshot for above issue: enter image description here
2.) I want to bind a RadGrid using:

  • _SPID = Request.QueryString["SPID"]
  • Items getting in RadListBox2 (only selected items from RadListBox1)

right now I am binding RadGrid using only Request.QueryString["SPID"], so I get all the data related to "SPID"

Example:

SPID     RadListBox1_Items     Value

014     a           test1

014     b           test2

014     c           test3

My requirement is: If suppose in RadListBox2 got only 2 items "a" , "c" then the data in RadGrid shall bind based on RadListBox2 items and "SPID"

SPID     RadListBox2_Items     Value

014     a           test1

014     c           test3

I hope I made my requirement clear. Please let me know if any confusion. Please do reply for both the points.
Thanks in advance.


Solution

  • I think that below code will fulfill your Requirement

    protected void RadListBox2_Inserted(object sender, RadListBoxEventArgs e)
    {
        ArrayList myTest = new ArrayList();
        //StringBuilder s = new StringBuilder();
        for (int i = 0; i < e.Items.Count; i++)
        {
            myTest.Add(e.Items[i].Text);    
    
        } 
         foreach (string x in myTest)
            {
                    lblInsertedList2.Text += x + ", " + "&nbsp;";
            }
    }
    

    OR simply

    protected void RadListBox2_Inserted(object sender, RadListBoxEventArgs e)
    {
        for (int i = 0; i < e.Items.Count; i++)
        {
            lblInsertedList2.Text += e.Items[i].Text + ", " + "&nbsp;";
        } 
    
    }
    

    Hopes it helps...