C# 将复选框设置为已签入中继器/复选框列表
我正在使用中继器显示来自web服务的一些数据 我的中继器结构是:C# 将复选框设置为已签入中继器/复选框列表,c#,asp.net,repeater,checkboxlist,asprepeater,C#,Asp.net,Repeater,Checkboxlist,Asprepeater,我正在使用中继器显示来自web服务的一些数据 我的中继器结构是: <asp:Repeater ID="rptgrp" runat="server"> <ItemTemplate> <asp:CheckBoxList ID="chkBoxListGoup" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "Titles")%>'
<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>
现在,我的web服务以“标题”的形式返回这些字段:
1) 头衔
2) IDTitle
3) iUserTitle
现在,我想在isUserTitle=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;
}
}
}
}
protectedvoid Repeater\u ItemDataBound(对象发送方,RepeaterItemEventArgs e){
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem)
{
CheckBoxList chklst=(CheckBoxList)e.Item.FindControl(“chkBoxListGoup”);
对于(int i=0;i
以下是示例代码,演示了该思想:
代码隐藏:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.UI.WebControl;
使用WebApp.RepeaterCheckboxList.TODODatasetTableAdapters;
命名空间WebApp.repeatercheckbox列表
{
公共部分类WebForm1:System.Web.UI.Page
{
数字视图;
IEnumerable子视图;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
TasksViewTableAdapter adp=新的TasksViewTableAdapter();
var dt=adp.GetData();
view=dt.AsEnumerable();
变量名称=(从视图中的x开始)
选择新的
{
Person=x.姓名,
ID=x.PersonID
}).Distinct();
DataList1.DataSource=名称;
DataList1.DataBind();
}
}
受保护的void CheckBoxList1_数据绑定(对象发送方,事件参数e)
{
CheckBoxList=(CheckBoxList)发送方;
var person=((DataListItem)theList.Parent).DataItem为动态;
变量名称=person.person;
var id=person.id;
var vw=子视图;
对于(int i=0,j=vw.Count();i
Aspx:
尝试将选中的值设置为被评估的对象
<asp:Repeater ID="rptgrp" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="chkBoxListGoup" runat="server"
Checked=<%# Eval("isUserTitle") %>>
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
My复选框包含文本形式的“标题”。考虑到我不能重写文本属性,在那里我必须把“iSueSead”放在哪里?如何检查?谢谢
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);
}
}
}
<%@ 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>
<asp:Repeater ID="rptgrp" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="chkBoxListGoup" runat="server"
Checked=<%# Eval("isUserTitle") %>>
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>