复选框改变状态?如何找出哪些复选框已更改其状态
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 保存在列表中。我该如何继续。
提前致谢。即使您能给我一些想法,我也会很感激。再次感谢!
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>
{
}
}
So far so good. I'm handling the OnItemDataBound event to check those textboxes that show profiles with the property Profile.StatusID set to 1.
Now i want to capture all changes. Say if a user unchecks a checkbox or checkes a previously unchecked checkbox I'd like to save the ID's of those Profiles in a list. How do I proceed.
Thanks in advance. I'd appreciate even if you give me ideas to revolve around. Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要处理 复选框更改事件< /a> 之后您可以确定它是否是您的收藏,并相应地添加或删除它
You need to handle the Checkbox changed event after this you can determine if it is your collection or not and add or remove it accordingly