如何从asp:Repeater遍历项目模板中的项目? [英] How can I loop through Items in the Item Template from an asp:Repeater?

查看:74
本文介绍了如何从asp:Repeater遍历项目模板中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,该中继器与项目绑定在preRender上.在项目模板中,每一行都有一个复选框.效果很好.

I have a repeater, which is bound on preRender with items. In the Item template each row has a check box. This works fine.

在绑定项目模板后,我试图遍历项目模板中的所有复选框.有什么办法吗?

I'm trying to loop through all the checkboxes in the item template after it has been bound. Is there any way of doing this?

推荐答案

在我看来,您想使用ItemDataBound事件.

It sounds to me like you want to use the ItemDataBound event.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

您将要检查RepeaterItem的ItemType,以便不要尝试在Header/Footer/Seperator/Pager/Edit中找到复选框

You will want to check the ItemType of the RepeaterItem so that you don't attempt to find the checkbox in Header/Footer/Seperator/Pager/Edit

您的事件看起来类似于:

Your event would look something along the lines of:

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;
    }
}

可以通过在代码中添加事件来引发此事件,如下所示:

This event can be raised by adding the event in your code behind like so:

rptItems.ItemDataBound += new RepeaterItemEventHandler(rptItems_ItemDataBound);

或通过将其添加到客户端上的控件中:

Or by adding it to the control on the client:

onitemdatabound="rptItems_ItemDataBound"


或者,您也可以按照其他建议的方式进行操作,并在RepeaterItems上进行迭代,但是您仍然需要检查项目类型.


Alternatively you can do as the others suggested and iterate over the RepeaterItems, however you still need to check the 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;
    }
}

绑定中继器后,您想在 Page PreRender 中执行此操作.

You would want to do that in the Page PreRender, after the Repeater has been bound.

这篇关于如何从asp:Repeater遍历项目模板中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆