using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Repeater_Checkbox
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
repeater.DataSource = PopulateCollection();
repeater.DataBind();
}
public CollectionProfiles PopulateCollection()
{
var lista = new CollectionProfiles();
var p1 = new Profile {ProfileDesc = "asdas", ProfileID = 1, ProfileStatus = 1};
lista.Add(p1);
var p2 = new Profile {ProfileDesc = "asdasd", ProfileID = 2, ProfileStatus = 0};
lista.Add(p2);
var p3 = new Profile {ProfileDesc = "nsadsdot", ProfileID = 3, ProfileStatus = 1};
lista.Add(p3);
var p4 = new Profile {ProfileDesc = "gluposti", ProfileID = 4, ProfileStatus = 1};
lista.Add(p4);
var p5 = new Profile {ProfileDesc = "asdaile", ProfileID = 5, ProfileStatus = 0};
lista.Add(p5);
var p6 = new Profile {ProfileDesc = "sdfsdf", ProfileID = 6, ProfileStatus = 1};
lista.Add(p6);
var p7 = new Profile {ProfileDesc = "dfsdf", ProfileID = 7, ProfileStatus = 1};
lista.Add(p7);
return lista;
}
protected void repeater_ItemDataBound(object source, RepeaterItemEventArgs e)
{
var taaLista = PopulateCollection();
var someItem = (CheckBox)e.Item.FindControl("checkbox");
var profileID = Convert.ToInt32(someItem.Attributes["data-id"]);
foreach (var item in taaLista)
{
if ((item.ProfileID == profileID) && (item.ProfileStatus == 1))
{
someItem.Checked = true;
return;
}
someItem.Checked = false;
}
}
public class CollectionProfiles : Collection<Profile>
{
}
}
到目前为止,一切都很好。我正在处理 OnItemDataBound 事件以检查那些显示配置文件的文本框,其属性 Profile.StatusID 设置为 1。
现在我想捕捉所有的变化。假设用户取消选中复选框或选中以前未选中的复选框,我想将这些配置文件的 ID 保存在列表中。我该如何进行。
提前致谢。即使你给我一些想法,我也会很感激。再次感谢!