Repeater或DataList中的复选框OnClick / ItemCommand

当在转发器控件中单击CheckBox时,我需要在转发器中的一行上执行一些服务器端逻辑。

有谁知道如何去做?

我看到它的方式你不能解雇项目命令,如果你使用CheckBoxes OnClick你不能得到转发器行。

这是我过去做过类似事情的快速模型。

     

代码隐藏:

  public class Model { public int Id { get; set; } public string Name { get; set; } } public partial class Checkboxes : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack ) { repeater1.DataSource = new List { new Model { Id = 1, Name = "a" }, new Model { Id = 2, Name = "b" }, new Model { Id = 3, Name = "c" } }; repeater1.DataBind(); } } protected void repeater1_OnItemDataBound(Object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { var item = e.Item.DataItem as Model; if (item != null) { var chk = e.Item.FindControl("chk") as CheckBox; if (chk != null) { chk.Text = item.Name; chk.InputAttributes.Add("value", item.Id.ToString()); } } } } protected void Check_Changed(Object sender, EventArgs e) { var id = ((CheckBox) sender).InputAttributes["value"]; //you now have access to the item id and can manipulate at will. } } 

试试这个代码隐藏:

  protected void Checked_Changed(object sender, EventArgs e) { var item = ((CheckBox)sender).Parent as RepeaterItem; // now you have the repeater row. You can travers further up the controls if you use Parent.Parent... } 

您可以使用OnClick事件循环遍历转发器中的每个项目,并检查每个复选框的值,(IsChecked == true)。

只需确保您没有在转发器上调用“DataBind()”,否则可能会导致问题。