【问题标题】:Finding a dynamically added control in a third nested repeater在第三个嵌套中继器中查找动态添加的控件
【发布时间】:2012-01-08 16:07:34
【问题描述】:

我有三个中继器。我们称它们为 R1、R2 和 R3。

R3 中,我在Page_Init 中创建了一堆控件。例如,其中一个控件的名称为 WMC_image

我正在使用此代码获取 R2:

Page.FindControl("R1").Controls[1].FindControl("R2")

这很好用。但是当我添加代码得到 R3 时:

Page.FindControl("R1").Controls[1].FindControl("R2").Controls[1].FindControl("R3")

我得到错误:

"System.ArgumentOutOfRangeException: 指定的参数超出了 有效值的范围。参数名称:index”。

我怎么会收到这个错误?以及如何在 R3 中获取 HtmlGenericControl?

编辑:抱歉,我忘了在 Page_Init 中添加只有 R1 填充 DataBind()。其他两个中继器在 OnItemDataBound 事件中填充。

当我循环出 R1 中的所有控件时,我得到了一堆命中。但即使我可以在我的网站上看到它们,我也没有从 R2 获得任何信息。

【问题讨论】:

  • 这就是为什么不建议在同一行使用多个点的原因。试试这个: Control r1 = Page.FindControl("R1"); ControlCollection rcCol = r1.Controls[1];控制 r2 = rcCo.FindControl("R2"); ControlCollection rc2Col = r2.Controls[1];控件 r3 = rc2Col.FindControl("R3");
  • 谢谢!但是:“不能将类型'System.Web.UI.Control'隐式转换为'System.Web.UI.ControlCollection'”
  • ControlCollection 似乎不适用于 FindControl?至少我不能那样使用它..

标签: .net findcontrol datarepeater


【解决方案1】:

您需要在多行中执行此操作,而不是在一行中执行此操作:

您可能还想添加一些错误检查

Control R1;
Control R2;
Control R3;    

if (Page.HasControls()) {
  R1 = Page.FindControl("R1").Controls[1]
  if ( R1.HasControls()) {
    R2 = R1.FindControl("R2").Controls[1]
    if (R2.HasControls()) {
      R3 = R2.FindControl("R3")
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多