简体   繁体   English

将复选框设置为在Repeater / CheckboxList中选中

[英]Set checkbox as checked in Repeater/CheckboxList

I'm using a Repeater to show some data coming from a web service. 我正在使用中继器来显示来自Web服务的一些数据。

My Repeater structure is: 我的Repeater结构是:

<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 ": 现在,我的Web服务将在“ 标题 ”中返回以下字段:

1) Title 1)标题

2) IDTitle 2)IDTitle

3) isUserTitle 3)isUserTitle

Now, I would like to set checked a checkbox when isUserTitle is = 1. 现在,我想在isUserTitle = 1时设置选中的复选框。

How can I do that? 我怎样才能做到这一点?

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 在itemdatabound中找到复选框列表,使用循环检查每个复选框列表的项目文本,选择文本为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;
        }
    }
  }
}

Try changing <asp:CheckBoxList ID="chkBoxListGoup" runat="server" to 尝试将<asp:CheckBoxList ID="chkBoxListGoup" runat="server"更改为

<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是否选中,否则如果value = 0未选中...

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: 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 如果您对数据感兴趣,请单击此处

Try just setting the Checked value to the object being Eval ed. 尝试仅将Checked值设置为要Eval的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM