ASP.NET直放站 - 通过在项目模板项目环 [英] ASP.NET Repeater - loop through Items in the Item Template

查看:119
本文介绍了ASP.NET直放站 - 通过在项目模板项目环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,这是与项目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.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx\">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

您将要检查的RepeaterItem的ItemType的,这样你不要试图找到在页眉/页脚/分离器/传呼机/编辑复选框

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

这事件可以通过在code添加事件背后,像这样得到提升:

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

您也想这样做,在页preRender ,直放站已经绑定了。

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

这篇关于ASP.NET直放站 - 通过在项目模板项目环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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