Csharp/C#教程:Repeater或DataList中的复选框OnClick / ItemCommand分享


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()”,否则可能会导致问题。

上述就是C#学习教程:Repeater或DataList中的复选框OnClick / ItemCommand分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1001050.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐