Set checkbox as checked in Repeater/CheckboxList

4.2k views Asked by At

I'm using a Repeater to show some data coming from a web service.

My Repeater structure is:

<asp:Repeater ID="rptgrp" runat="server">
   <ItemTemplate>
     <asp:CheckBoxList ID="chkBoxListGoup" runat="server"
          DataSource='<%# DataBinder.Eval(Container.DataItem, "Titles")%>' 
          DataTextField="Title"
          DataValueField="IDTitle">
      </asp:CheckBoxList>                
   </ItemTemplate>
 </asp:Repeater>

Now, my web service returns these fields in "Titles":

1) Title

2) IDTitle

3) isUserTitle

Now, I would like to set checked a checkbox when isUserTitle is = 1.

How can I do that?

4

There are 4 answers

0
deostroll On

Here is sample code that demonstrates the idea:

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using WebApp.RepeaterCheckboxList.TODODatasetTableAdapters;

namespace WebApp.RepeaterCheckboxList
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        IEnumerable<TODODataset.TasksViewRow> view;
        IEnumerable<TODODataset.TasksViewRow> subview;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TasksViewTableAdapter adp = new TasksViewTableAdapter();
                var dt = adp.GetData();
                view = dt.AsEnumerable();
                var names = (from x in view
                             select new
                             {
                                 Person = x.Name,
                                 ID = x.PersonID
                             }).Distinct();
                DataList1.DataSource = names;
                DataList1.DataBind();

            }
        }

        protected void CheckBoxList1_DataBound(object sender, EventArgs e)
        {
            CheckBoxList theList = (CheckBoxList)sender;
            var person = ((DataListItem)theList.Parent).DataItem as dynamic;
            var name = person.Person;
            var id = person.ID;

            var vw = subview;
            for (int i = 0, j = vw.Count(); i < j; i++)
            {
                var task = vw.ElementAt(i);
                theList.Items[i].Selected = task.Completed;
            }
        }        

        protected IEnumerable<TODODataset.TasksViewRow> GetTasks(object data)
        {
            var vw = data as dynamic;
            return subview = (from x in view
                    where x.PersonID == vw.ID
                    select x);

        }
    }
}

Aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.RepeaterCheckboxList.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                <div style="padding:5px">
                    <h3><%# Eval("Person") %></h3>
                    <div>
                        <asp:CheckBoxList OnDataBound="CheckBoxList1_DataBound" ID="CheckBoxList1" 
                            runat="server" 
                            DataTextField="TaskDesc" DataValueField="TaskID"
                            DataSource="<%# GetTasks(Container.DataItem) %>"></asp:CheckBoxList>
                    </div>
                </div>
            </ItemTemplate>
        </asp:DataList>

    </div>
    </form>
</body>
</html>

If you are interested in the data, click here

0
Volearix On

Try just setting the Checked value to the object being Evaled.

<asp:Repeater ID="rptgrp" runat="server">
   <ItemTemplate>
     <asp:CheckBoxList ID="chkBoxListGoup" runat="server"
          Checked=<%# Eval("isUserTitle") %>>
      </asp:CheckBoxList>                
   </ItemTemplate>
 </asp:Repeater>
0
Simon Price On

Try changing <asp:CheckBoxList ID="chkBoxListGoup" runat="server" to

<asp:CheckBoxList ID="chkBoxListGoup" Checked='<%#Eval("Flag")%>' runat="server"

Flag being your Column..

Then in your method or event handler you want to run some code to say if this value = 1 checked, elseif value = 0 unchecked...

1
Vikas Rana On

You can find checkboxlist as follows

Find checkboxlist in itemdatabound, check item text of every checkboxlist using loop, select the item whose text is 1

Protected void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
  {
    CheckBoxList chklst = (CheckBoxList)e.Item.FindControl("chkBoxListGoup");
    for (int i = 0; i < chk.Items.Count; i++)
    {
        if (chk.Items[i].Text == "1")
        {
            chk.Items[i].Selected = true;
        }
    }
  }
}