More in this section

Forums / Developing with Sitefinity / Set Multiple selected values in Checkboxlist

Set Multiple selected values in Checkboxlist

4 posts, 0 answered
  1. antonio romero
    antonio  romero avatar
    2 posts
    Registered:
    27 Jan 2010
    27 Jan 2010
    Link to this post

    Hi
    I have a checkbox list bound to a linq to sql datasource. I want to be able to set the selected items on my checkboxlist but cannot.

    If I try to set the SelectedValue to a value it only displays the last value as selected instead of all the values which are selected. How can I select multiple values in my checkboxlist ?

    here is the code:
    .ascx

    <asp:CheckBoxList runat="server" ID="CheckBoxListPotencialidad" DataSourceID="LinqDataSource4" DataTextField="NombrePotencialidad" DataValueField="IdPotencialidad" CssClass="Clase_CheckBoxList">   
    </asp:CheckBoxList>   
     

     

     

     

    .ascx.cs  

     

    protected void LoadContact(string codigo)   
    {  
      var db = new Guadalquivir.GuadalquivirDataClassesDataContext();  
      var ficha = (from c in db.Ficha   
                   where c.Codigo == codigo.ToString()   
                   select c).Single();  
     
      var listaPotencialidad = (from d in db.FichaPotencialidadAsociada   
                                where d.CodigoFicha == ficha.Codigo   
                                select d.IdPotencialidad);   
       
       Nombre.Text = ficha.Nombre.ToString();  
       Codigo.Text = ficha.Codigo.ToString();  
       Repositorio.Text = ficha.Repositorio;  
     
      foreach (byte idPotencialidad in listaPotencialidad) {   
        CheckBoxListPotencialidad.SelectedValue = Convert.ToString(idPotencialidad);   
        CheckBoxListPotencialidad.Items.Add(CheckBoxListPotencialidad.SelectedValue);  
      }  
     }
     

     

     

     

    Tranks in advance.
    Antonio Romero

     

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jan 2010
    Link to this post
    Hello antonio romero,

    SelectedValue property only checks an item at a time.  Loop through the ListItemcollection and check if the Selected property of a ListItem returns true. Just to let you know RadEditor supports FormatCodeBlock have to be used when you paste your code.


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. antonio romero
    antonio  romero avatar
    2 posts
    Registered:
    27 Jan 2010
    27 Jan 2010
    Link to this post
    Thank You Ivan
    I´ve edited my post using "Format Code Block".

    What i´m triying to do is to get from the Table db.FichaPotencialidadAsociada some values and render the form with those values selected in the Checkboxlist. So the selected property won´t be true.

    I want to be able to set the selected items on my checkboxlist on the loadContact method but cannot.

    The datasource's selected event fires after the page load. If this is the case, how can I set the selected items on my list?

    Thanks
    Antonio.

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jan 2010
    Link to this post
    Hello antonio romero,

    Why do not try to move the code OnPreRender? Also, this problem does not seems to be related to Sitefinity. It is related to binding CheckBoxList

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
4 posts, 0 answered