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

Retrieving the checkbox data in a listbox

3 Answers 203 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Anjali
Top achievements
Rank 1
Anjali asked on 03 May 2011, 09:15 PM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 May 2011, 07:49 AM
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.
0
Anjali
Top achievements
Rank 1
answered on 04 May 2011, 10:57 PM
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(); 
0
Anjali
Top achievements
Rank 1
answered on 05 May 2011, 04:19 PM
Hi Princy,

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

Thanks again for all you help!
Tags
ListBox
Asked by
Anjali
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Anjali
Top achievements
Rank 1
Share this question
or