Retrieving the checkbox data in a listbox

4 posts, 1 answers
  1. Anjali
    Anjali avatar
    101 posts
    Member since:
    Dec 2010

    Posted 03 May 2011 Link to this post

    Hi All,

      I have a listbox with two columns of checkboxes in it. I am binding the list to the database and the data is being dispalyed, but none of the checkboxes is cheked. how can I retreive the checkboxes data for the listbox. Below is my code for binding the listbox

    public void populate_selectedAssistAgency(String casRepId)
    {
        Model.rep.repHeader RepHeader = new Model.rep.repHeader();
        AssistAgencyAdapter adp = new AssistAgencyAdapter();
        List<Assist> assist = new List<Assist>();
        RepHeader.repId = RepId;
        RepHeader = adp.getSelectedAssistAgency(RepHeader);
        assist = RepHeader.RepAssistAgency;
        RadListBox RadListBox_selectedAssistAgency = (RadListBox)RadPanelBar1.FindItemByText("RepIssues").Items[0].FindControl("RadListBox_selectedAssistAgency");
        RadListBox_selectedAssistAgency.DataSource = assist;
        RadListBox_selectedAssistAgency.DataTextField = "Assist_location_name";
        RadListBox_selectedAssistAgency.DataValueField = "Assist_location_id";
        RadListBox_selectedAssistAgency.DataBind();
    }

     and my aspx code loks like this
    <td style="width:"2%">t<br />
               <telerik:RadListBox runat="server" ID="RadListBox_selectedAssistAgency" AllowDelete="false" AllowReorder="false"
                   AutoPostBack="false" SelectionMode="Multiple" Width="120px" Height="200px"   >
                   <ItemTemplate>
                     
                      <asp:CheckBox ID="chkAssistDistance" runat="server" />
                       <asp:CheckBox ID="chkAssistOnSite" runat="server" />
                       <asp:Label ID="lblAssistAgencySelected" runat="server" Text='<%# DataBinder.Eval(Container,"Text") %>'></asp:Label>
                   </ItemTemplate>
                    
               </telerik:RadListBox>

    Any help will be apprecaited.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 May 2011 Link to this post

    Hello Nitu,

    One suggestion is to attach ItemDataBound event and set the state of the checkBox from there.
    Here is a sample code:
    C#:
    protected void RadListBox_selectedAssistAgency_ItemDataBound(object sender, RadListBoxItemEventArgs e)
      {
          DataRowView dataSourceRow = (DataRowView)e.Item.DataItem;
          if ((dataSourceRow["bool"].ToString().ToLower()).Trim().Equals("true"))//bool is the database field name
          {
              CheckBox checkbox = (CheckBox)e.Item.FindControl("chkAssistOnSite");//accessing the checkbox and setting the state.
              checkbox.Checked = true;
          }
      }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Anjali
    Anjali avatar
    101 posts
    Member since:
    Dec 2010

    Posted 04 May 2011 Link to this post

    The above  didn't work. is it possible to do it right in method populate_SelectedAssistAgency? You mentioned in your answer one suggestions is.... is their any other way? I really need help with this.

    Any help will be greatly appreciated.
    public void populate_selectedAssistAgency(String RepId) 
        Model.rep.repHeader RepHeader = new Model.rep.repHeader(); 
        AssistAgencyAdapter adp = new AssistAgencyAdapter(); 
        List<Assist> assist = new List<Assist>(); 
        RepHeader.repId = RepId; 
        RepHeader = adp.getSelectedAssistAgency(RepHeader); 
        assist = RepHeader.RepAssistAgency; 
        RadListBox RadListBox_selectedAssistAgency = (RadListBox)RadPanelBar1.FindItemByText("RepIssues").Items[0].FindControl("RadListBox_selectedAssistAgency"); 
        RadListBox_selectedAssistAgency.DataSource = assist; 
        RadListBox_selectedAssistAgency.DataTextField = "Assist_location_name"; 
        RadListBox_selectedAssistAgency.DataValueField = "Assist_location_id"; 
        RadListBox_selectedAssistAgency.DataBind(); 
  5. Anjali
    Anjali avatar
    101 posts
    Member since:
    Dec 2010

    Posted 05 May 2011 Link to this post

    Hi Princy,

    I got it working. I got the basic idea from your post.

    Thanks again for all you help!
Back to Top