This is a migrated thread and some comments may be shown as answers.

listbox in codebehind

27 Answers 456 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Troika
Top achievements
Rank 1
Troika asked on 21 Feb 2013, 01:40 PM
i have this listboxwith icons and the arrows to transfer one to left or all to left or one to rigth or all to rigth.
how can i detect wich arrow user as clicked in code behind becausee e see the demo and it doesnt show how it works

so i want to load to a left listbox all items from specific sql datasource and then the rigth lisbox just have the one that the user move to rigth listbox so i waant to associate items but they are loaded from diferent sources

27 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Feb 2013, 06:18 AM
Hi Troika

Try the following JS and code snippet to identify which arrow(transfer from or transfer all from) is clicked.
aspx:
<telerik:RadListBox ID="RadListBox1" runat="server" AllowTransfer="true" AutoPostBackOnTransfer="true"
    TransferToID="RadListBoxDestination" OnTransferring="RadListBox1_Transferring"
    OnClientTransferring="clientTransfering">
    <Items>
        <telerik:RadListBoxItem Text="001" />
          .............................
    </Items>
</telerik:RadListBox>
<telerik:RadListBox runat="server" ID="RadListBoxDestination">
</telerik:RadListBox>
JS:
<script type="text/javascript">
function clientTransfering(sender, args)
{
    var target = args.get_domEvent().target.parentNode;
    while (target.nodeName != "A")
    target = target.parentNode;
    sender.trackChanges();
    if (target.className.indexOf("rlbTransferFrom") > -1)
        sender.get_items().getItem(0).get_attributes().setAttribute("clickedButton", "transferFrom");
    else if (target.className.indexOf("rlbTransferAllFrom") > -1)
        sender.get_items().getItem(0).get_attributes().setAttribute("clickedButton", "transferAllFrom");
    sender.commitChanges();
}
</script>
C#:
protected void RadListBox1_Transferring(object sender, Telerik.Web.UI.RadListBoxTransferringEventArgs e)
{
    String transferButton = (sender as RadListBox).Items[0].Attributes["clickedButton"].ToString();
}

Thanks
Princy
0
Troika
Top achievements
Rank 1
answered on 22 Feb 2013, 09:40 AM
i need the 4 options:

- all to left
-all to rigth
-one to left
-one to rigth

also i need to pick the icons from a specific folder and read wich icon to asociate with the item from database so icon a is with item a but this values are fom database

also this is not catching the 4 events in codebehind i need to catch them to make diffenrtent actions and write chenges to my db so this in code behind.
String transferButton = (sender as RadListBox).Items[0].Attributes["clickedButton"].ToString();

just gets the clicked button and i need if its all to rigth do some action if all to left do other.....
0
Troika
Top achievements
Rank 1
answered on 22 Feb 2013, 10:44 PM

also i have this code to try to populate the listboxes but they wont populate i'm returning a datatable in sql command:

protected void ActualizarListas()
         {
 
                int id_atleta = (int)RadGrid1.SelectedValues["id"];
 
                RadListBox1.DataTextField = "Nome";
                RadListBox1.DataValueField = "id";
                RadListBox1.DataSource = BLL.Patrocinadores.ListarTodosPatrocinadoresNaoAssociados(id_atleta);
                RadListBox2.DataTextField = "Nome";
                RadListBox2.DataValueField = "id";
                RadListBox2.DataSource = BLL.Patrocinadores.ListarTodosPatrocinadoresAssociados(id_atleta);
 
         }
       
        protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
            ActualizarListas();
 
        }
the sql code works fine but they are not getting populated... also i want that the icon is red from database sql command (fotos.url_img) and loaded from a image folder

so iconA match listbox itemA etc

public static DataTable ListarTodosPatrocinadoresAssociados(int id_atleta)
        {
          
            ArrayList _arraySqlParams = new ArrayList();
 
            _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
 
            return (SQLHelper.ExecuteDataTable("SELECT TOP (100) PERCENT dbo.Patrocinador.nome, dbo.Patrocinador.id, dbo.Fotos.url_img FROM dbo.Patrocinador INNER JOIN dbo.Fotos ON dbo.Patrocinador.id_foto = dbo.Fotos.id WHERE(dbo.Patrocinador.id IN (SELECT id_patrocinador FROM   dbo.Atleta_Patrocinador WHERE (id_atleta = @id_atleta))) ORDER BY dbo.Patrocinador.nome", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
 
        }

0
Troika
Top achievements
Rank 1
answered on 23 Feb 2013, 11:14 AM
any help?

code updated but not working dont see nothing on radlistbox
RadListBox1.DataTextField = "Nome";
                RadListBox1.DataValueField = "id";
                RadListBox1.DataSource = BLL.Patrocinadores.ListarTodosPatrocinadoresNaoAssociados(id_atleta);
                RadListBox1.DataBind();
                RadListBox2.DataTextField = "Nome";
                RadListBox2.DataValueField = "id";
                RadListBox2.DataSource = BLL.Patrocinadores.ListarTodosPatrocinadoresAssociados(id_atleta);
                RadListBox2.DataBind();
0
Nencho
Telerik team
answered on 26 Feb 2013, 02:10 PM
Hello,

If you are trying to bind RadListBox from the code behind please review this documentation article where this functionality is explained.

As for your other question would you please elaborate what exactly do you mean by -"also i need to pick the icons from a specific folder and read wich icon to asociate with the item from database so icon a is with item a but this values are fom database" because it is not quite clear, so we could be more helpful.


All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 28 Feb 2013, 10:17 PM
ok i will load the listboxes with two datasources and this datasource have a field that i call url_img wich is the name of the small icon suposed to show in this listboxes and then i have in the folder images that images so it reads in from the command and show it near by the name

so it read on db img1, then pick on folder img1 to show in this radlistbox

also need to know how i can work with the 4 option in codebehind i havent understood very well
what I need is to access the trsnsfer buttons individually to run a sql command to transfer between listboxes
if you can provide and example I would appreciate that
0
Troika
Top achievements
Rank 1
answered on 04 Mar 2013, 04:33 PM
any help?
0
Nencho
Telerik team
answered on 05 Mar 2013, 03:07 PM
Hello Troika,

I can suggest you to use the OnItemDataBound event handler, in order to access the DataItem and assign the ImageUrl to the RadListBoxItem in the following manner :

protected void SourceListBox_ItemDataBound(object sender, RadListBoxItemEventArgs e)
   {
       e.Item.ImageUrl = (e.Item.DataItem as DataRowView)["ImagePathInFolder"].ToString();
   }

As for you second question, you could use the OnTransferred event handler and easily determine which button was clicked, regarding the count of the transferred items (e.Items.Count) and the SourceListBox(e.SourceListBox).


Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 09 Mar 2013, 06:13 PM
what items count is related with the transfer arrows? i need to detect if user click on arrow left, arrow rigth, all rigth, all left and run diferente sql command depending on wich arrow is seleted so with that i need to detect the arrow.

hope you understood better, a user previously post at the begin of this post a JS detected mode but how can i related that in codebehind?
0
Troika
Top achievements
Rank 1
answered on 10 Mar 2013, 05:49 PM
public static DataTable ListarTodosPatrocinadoresNaoAssociados(int id_atleta) // listbox1
       {
           
           ArrayList _arraySqlParams = new ArrayList();
 
           _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
 
 
            DataTable r=(SQLHelper.ExecuteDataTable("SELECT TOP (100) PERCENT dbo.Patrocinador.nome, dbo.Patrocinador.id, dbo.Fotos.url_img FROM dbo.Patrocinador INNER JOIN dbo.Fotos ON dbo.Patrocinador.id_foto = dbo.Fotos.id WHERE(dbo.Patrocinador.id NOT IN (SELECT id_patrocinador FROM   dbo.Atleta_Patrocinador WHERE (id_atleta = @id_atleta))) ORDER BY dbo.Patrocinador.nome", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
            return r;
 
       }
 
 
       public static DataTable ListarTodosPatrocinadoresAssociados(int id_atleta) listbox2
       {
         
           ArrayList _arraySqlParams = new ArrayList();
 
           _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
 
          DataTable r= (SQLHelper.ExecuteDataTable("SELECT TOP (100) PERCENT dbo.Patrocinador.nome, dbo.Patrocinador.id, dbo.Fotos.url_img FROM dbo.Patrocinador INNER JOIN dbo.Fotos ON dbo.Patrocinador.id_foto = dbo.Fotos.id WHERE(dbo.Patrocinador.id IN (SELECT id_patrocinador FROM   dbo.Atleta_Patrocinador WHERE (id_atleta = @id_atleta))) ORDER BY dbo.Patrocinador.nome", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
          return r;
       }
 
       public static int Remover1PatrocinadorAoAtleta(int id_atleta, string id_patrocinador) // remove one
       {
           ArrayList _arraySqlParams = new ArrayList();
 
           _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
           _arraySqlParams.Add(new SqlParameter("id_patrocinador", id_patrocinador));
 
 
 
           return (SQLHelper.ExecuteNonQuery("delete from atleta_patrocinador where id_patrocinador=@id_patrocinador and id_atleta=@id_atleta", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
       }
 
 
 
       public static int RemoverTodosOsPatrocinadoresAoAtleta(int id_atleta, string id_patrocinador) / remove all - transfer all
       {
           ArrayList _arraySqlParams = new ArrayList();
 
           _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
           _arraySqlParams.Add(new SqlParameter("id_patrocinador", id_patrocinador));
 
 
 
           return (SQLHelper.ExecuteNonQuery("delete from atleta_patrocinador where id_atleta=@id_atleta", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
       }
 
 
 
       public static int Adicionar1PatrocinadorAoAtleta(int id_atleta, string id_patrocinador)
       {
           ArrayList _arraySqlParams = new ArrayList();
 
           _arraySqlParams.Add(new SqlParameter("id_atleta", id_atleta));
           _arraySqlParams.Add(new SqlParameter("id_patrocinador", id_patrocinador));
 
 
 
           return (SQLHelper.ExecuteNonQuery("insert into atleta_patrocinador(id_atleta, id_patrocinador) values (@id_atleta, @id_patrocinador)", (SqlParameter[])_arraySqlParams.ToArray(typeof(SqlParameter))));
       }
i really need fast help on this so if you provide na exemple demo
i need to bind the 2 listbox with icons and the values and then remove one or all / or add all add one depending on what user clicks

SO here are my sql commands:  - hope you have understood why i need to know wich arrow user clicks

so codebehind would be similar to this but in c#, i just need to pick on the corrects arrows and i'm not getting how to pick the arrow clicked event
Private Sub add_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_btn.Click
       BR.Actores.Adicionar1ActorAoFilme(id_filme, lbx_todos_os_actores.SelectedValue)
       ActualizarListas()
 
   End Sub
 
   Private Sub add_all_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_all_btn.Click
       BR.Actores.AdicionarTodososActoresAoFilme(id_filme)
       ActualizarListas()
   End Sub
 
   Private Sub remove_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles remove_btn.Click
       BR.Actores.Remover1ActorAoFilme(id_filme, lbx_actores_deste_filme.SelectedValue)
       ActualizarListas()
   End Sub
 
   Private Sub remove_all_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles remove_all_btn.Click
       BR.Actores.RemoverTodososActoresAoFilme(id_filme)
       ActualizarListas()
   End Sub
 
   Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
       Me.Close()
   End Sub
   Sub ActualizarListas()
       lbx_todos_os_actores.DisplayMember = "Nome"
       lbx_todos_os_actores.ValueMember = "Id_Actor"
       lbx_todos_os_actores.DataSource = BR.Actores.MostrarTodosOsActores(id_filme)
       lbx_actores_deste_filme.DisplayMember = "Nome"
       lbx_actores_deste_filme.ValueMember = "Id_Actor"
       lbx_actores_deste_filme.DataSource = BR.Actores.MostrarActoresAssociadosAoFilme(id_filme)
 
   End Sub


0
Nencho
Telerik team
answered on 13 Mar 2013, 04:24 PM
Hello Troika,

I have prepared a sample project for you, demonstrating how to determine which arrow was just clicked. Please find the sample attached.

Note that all dll files are removed.


Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 17 Mar 2013, 06:25 PM
I always have items.count==0 but the are items showing on the listbox , what I need to fetch is selectedvalue on the selected item in the listbox when transferring one and cant find a way.
I'm always getting items count==0 



how can I load an icon near to the listbox I see this:

                //DirectoryInfo img24Dir = new DirectoryInfo(Server.MapPath("~/uploads/images/miniaturas/"));
                //foreach (FileInfo file in img24Dir.GetFiles("*.png"))
                //{
                //    RadListBoxItem item = new RadListBoxItem(file.Name.Replace(".png", ""));
                //    item.ImageUrl = "~/uploads/images/miniaturas/" + file.Name;
 
 
                //    RadListBox1.Items.Add(item);
                //}
however I'm binding datasource of listbox from database and I need to read a field in database ex: if it reads imgA loads icon imgAIcon
0
Troika
Top achievements
Rank 1
answered on 17 Mar 2013, 07:28 PM
here is full code i'm getting items==0 and I need to get selectedvalue of the seleted item, and get the icon of the item in listbox accounding to its name so if name in listbox is A load iconA

0
Nencho
Telerik team
answered on 20 Mar 2013, 05:11 PM
Hello Troika,

I have performed some tests locally, but I was unable to replicate the faced issue with the items' count, when the transfer operation is performed. Here is a video, demonstrating the behavior at my end. In addition, I am sending you the sample I have tested with. Could you specify if I had missed something?

Kind regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 22 Mar 2013, 11:47 AM
i'm not understanding what is happening here when i databound items to the listbox i get items.count, when i enter transferred event it wont give them but also i need the selected value when i transfer one item between listboxes I tried

e.SourceListBox.SelectedValue && e.SourceListBox.ClientID == "RadListBox1" but I cant compare them obviously so how I get the selectedvalue of the selected item on a specific listbox?

see image:

http://postimg.org/image/x53xmbljj/

0
Nencho
Telerik team
answered on 27 Mar 2013, 11:24 AM
Hello Troika,

Could you specify how your project differs from the previously provided sample, which behavior was demonstrated in the video?

Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 13 Apr 2013, 02:48 PM
i have a radgrid with postback when you seleted a item and it also updates the radlistbox the key point here is transfer to rigth and left is not working because items.coun is always=0 transfer all works but i need it to update the database while transfering

so you go to the gird seltect there a item then it shows associated articles and all articles not associated and when you transfer them it should update db

0
Nencho
Telerik team
answered on 17 Apr 2013, 03:39 PM
Hello Troika,

I am afraid that the provided code snippet is not runnable and I was unable to replicate the faced issue. Therefor, I would like to ask you to provide us with a runnable sample, demonstrating the issue you had faced, in order to inspect it locally.


Kind regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 18 Apr 2013, 08:42 AM
see here the vídeo: http://screencast.com/t/B6hojNmQfYl

the radbox's are connect to the database so when we click on the arrows it must run the commends i showed also in the videobu it doesnt detect some items in radbox
0
Nencho
Telerik team
answered on 23 Apr 2013, 07:47 AM
Hello Toika,

Would you specify why you are calling the ActualizarListas method, each time the Page_Load event is hit? As I can see, the method handles the population with data of both RadListBoxes and it is invoked each time a transfer operation is performed.


All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 25 Apr 2013, 08:58 AM
yes its correct, that method populates the listboxes with the associated sponsors (at right) and all non associated with the selected item (on left) so it just reads from a table in the db what I need is to get the selected item and transfer it between radlistboxes and run the method that I have already made I just need to know how to fetch the item (or all of them) depending on wich action is taken.
as you can see in the video sometimes count is zero.
so actualizarlistas just reads from database and populate the correct listbox
for example this method

 

BLL.Patrocinadores.Adicionar1PatrocinadorAoAtleta(id_atleta,id_patrocinador) is to associate a sponsor with the person selected in the gid but I need to pass id_patrocionador wich is the seletec item in the listbox wich I cant get because count it always zero. I have 4 methods depending on the action taken and other two to read from db

hope you have understood better now...

 

 

0
Bozhidar
Telerik team
answered on 29 Apr 2013, 08:22 AM
Hi,

The problem is caused by the fact that you are rebinding the ListBoxes on each postback too late in the page lifecycle, effectively clearing the selection in each one. And since the transfer operation relies on the selection, it fails. 

Try moving the rebinding logic in Page_Init.
 

Kind regards,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 29 Apr 2013, 08:46 AM
I have done what you said but then on the method ActualizarListas() in this line

int id_atleta = (int)RadGrid1.SelectedValues["id"

 

]; it breaks because the grid isn't created yet, I think so it gives the error reference is not an object...

also how to get the radlistbox selecteditem/value working with this:

else if (e.Items.Count > 1 && e.SourceListBox.ClientID == "RadListBox2")

 
0
Bozhidar
Telerik team
answered on 01 May 2013, 11:15 AM
Hi,

The else if (e.Items.Count > 1 && e.SourceListBox.ClientID == "RadListBox2")  statement fails, because the ClientID of the ListBox is actually not RadListBox2. This can be seen in you own video at 1:09 minutes, when you hover the ListBox. I've attached a screenshot, where you can see the actual ID of the ListBox.

As for the first issue, you can use a hidden field and set the Grid Selected value on the client, and in the Page_Init event use this hidden field to retrieve it. 
 

Regards,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 19 May 2013, 09:11 PM
ok now i understood why it hasn't changed if i use client id...
remember that the user as to select an item in the grid for the listboxes getting updated so I cant pass it on page_init because I don't know what user selects at that moment

could you provide a code to fix this?
0
Bozhidar
Telerik team
answered on 22 May 2013, 08:31 AM
Hi,

As I said in my previous reply, you can set the hidden field value on the client, when you know the Grid Selection, and then get it's value in pageInit. You can also use the following article as a reference:
http://forums.asp.net/t/1627237.aspx
 
Regards,
Bozhidar
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Troika
Top achievements
Rank 1
answered on 23 May 2013, 07:53 PM
but in the capture you see the wrong listbox why it dont change?
Tags
ListBox
Asked by
Troika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Troika
Top achievements
Rank 1
Nencho
Telerik team
Bozhidar
Telerik team
Share this question
or