Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从asp:Repeater循环项目模板中的项目?_C#_Asp.net_Repeater - Fatal编程技术网

C# 如何从asp:Repeater循环项目模板中的项目?

C# 如何从asp:Repeater循环项目模板中的项目?,c#,asp.net,repeater,C#,Asp.net,Repeater,我有一个转发器,它与项目绑定在preRender上。在项目模板中,每行都有一个复选框。这个很好用 在绑定项目模板后,我尝试循环遍历该模板中的所有复选框。有什么办法吗?试试这个 for each (RepeaterItem ri in Repeater1.Items) { CheckBox CheckBoxInRepeater = ri.FindControl("CheckBox1") as CheckBox; //do something with the checkbox

我有一个转发器,它与项目绑定在preRender上。在项目模板中,每行都有一个复选框。这个很好用

在绑定项目模板后,我尝试循环遍历该模板中的所有复选框。有什么办法吗?

试试这个

for each (RepeaterItem ri in Repeater1.Items)
{
     CheckBox CheckBoxInRepeater = ri.FindControl("CheckBox1") as CheckBox;

    //do something with the checkbox
}
for(int item=0;item
我想到了一些不同的想法:

  • 是否需要在preRender中绑定此转发器?请考虑使用PaqyLoad事件后更典型的绑定方式。

  • 为什么要在中继器绑定后查找复选框?使用此事件绑定时,您是否可以执行任何需要执行的操作:

    OnItemDataBound="Repeater1_OnItemDataBound"
    
  • 无论哪种方式,只要在中继器中进行迭代,就可以返回并查看其内部。请注意,如果复选框嵌套在repeater项中的其他元素中,则可能必须执行递归搜索

    for each (RepeaterItem r in Repeater1.Items) {
        CheckBox c = r.FindControl("CheckBox1") as CheckBox;
        //DO whatever
    }
    

  • 在我看来,您似乎想要使用ItemDataBound事件

    您需要检查RepeaterItem的ItemType,这样您就不会试图在页眉/页脚/分隔符/页面/编辑中找到复选框

    您的活动将大致如下:

    void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var checkBox = (CheckBox) e.Item.FindControl("ckbActive");
    
            //Do something with your checkbox...
            checkBox.Checked = true;
        }
    }
    
    可以通过在代码中添加事件引发此事件,如下所示:

    rptItems.ItemDataBound += new RepeaterItemEventHandler(rptItems_ItemDataBound);
    
    或者通过将其添加到客户端上的控件:

    onitemdatabound="rptItems_ItemDataBound"
    

    或者,您可以按照其他人的建议进行操作,并在RepeaterItems上进行迭代,但是仍然需要检查itemtype

    foreach (RepeaterItem item in rptItems.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            var checkBox = (CheckBox)item.FindControl("ckbActive");
    
            //Do something with your checkbox...
            checkBox.Checked = true;
        }
    }
    

    在绑定转发器后,您可能希望在“预渲染”页面中执行此操作。

    转发器上有很多生命周期事件,您可以点击,包括在创建和/或绑定每个项目时发生的事件,您不必等到结束,您可以直接在IDE中发现这些事件。使用这些复选框需要做什么?为什么要在preRender上绑定转发器?这已经很晚了,通常你会在页面加载阶段这样做。如果只想在数据绑定时循环中继器的所有行,中继器将是一个好地方,因为这不会在回发或客户端上导致额外的循环。这两种方法都是可能的,但首选项对于获得您可以使用的正确答案非常重要。@Tim,预渲染期间绑定的一个用例是您可能正在响应一个或多个其他事件。在这种情况下,加载是不合适的,事实上,其他事件处理程序可能不够
    foreach (RepeaterItem item in rptItems.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            var checkBox = (CheckBox)item.FindControl("ckbActive");
    
            //Do something with your checkbox...
            checkBox.Checked = true;
        }
    }