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

在绑定项模板之后,我try 遍历项模板中的所有复选框.有什么办法可以做到这一点吗?

推荐答案

听起来你想使用ItemDataBound事件.

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

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

你的活动可以大致如下:

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

你会想要在Page PreRender米中这样做,在转发器被绑定之后.

Asp.net相关问答推荐

无法在Microsoft Windows Server 2016(数据中心)上运行.NET可移植性分析器

Web 服务器配置为不列出此目录的内容. asp.net vs 2012 错误?

Web API Queryable - 如何应用 AutoMapper?

如何调试 Azure 500 内部服务器错误

带有 Web 套接字的 SignalR

有条件地在 Gridview 中隐藏 CommandField 或 ButtonField

将命令行参数传递给 ASP.NET Core 中的 Startup 类

RestSharp 获取请求的完整 URL

没有身份的 ASP.NET Core 2.0 承载身份验证

如何在 ASP.NET Web API 中使用非线程安全的 async/await API 和模式?

在 Asp.net Rowcommand 事件中获取行索引

从 C# AsyncCTP 使用 ExecuteReaderAsync 的任何缺点

IIS 会话超时与 ASP.NET 会话超时

发布网站项目时临时路径太长

回发后 Document.Ready() 不起作用

ASP.Net 错误:应用程序池的标识无效

ContentResult 与字符串

简单的 LINQ 和列表错误:WhereListIterator`1[Task]' to type 'System.Collections.Generic.List`1[Task]'

IE9 JavaScript 错误:SCRIPT5007:无法获取属性ui的值:对象为空或未定义

jQuery Ajax 调用 - 成功设置变量值