【问题标题】:Table filled with controls inside an ASP.NET FormView , get controls?表格中填充了 ASP.NET FormView 中的控件,获取控件?
【发布时间】:2010-02-04 23:33:17
【问题描述】:

在 FormView 中获取控件的技巧是什么。我正在使用 FindControl() 获取它们,但是现在我无法访问它们。示例:我在 FooterTemplate 上有一些 ImageButton,当涉及到 FormView 内的控件时,我可以顺利获得这些按钮!每个控件都为空。你认为我应该在每个模板中以不同的方式命名它们吗? 这让我想到造成这种噪音的桌子!

我正在使用 DataBound 事件并检查特定模式!有任何想法吗?谢谢。

[更新]

这是有效的

            if (this.kataSistimataFormView.CurrentMode == FormViewMode.Edit)
        {
            ImageButton update = (ImageButton)this.kataSistimataFormView.FindControl("btnUpdate");
            update.Visible = true;

但是由于某种原因没有

        CheckBox chkBoxPaidoi = kataSistimataFormView.FindControl("chkBoxPaidoi") as CheckBox;

【问题讨论】:

  • 所以你使用 FindControl 很好......然后它突然停止工作了?惊人!在此处使用 FindControl。
  • ImageButton 仍在工作,它只是在表格内的其他控件上返回 null。

标签: asp.net formview


【解决方案1】:

FindControl 不是递归的。我的意思是它只会找到您正在搜索的控件的子控件中的控件 - 它不会搜索子控件的任何子控件

如果您已将之前要查找的控件放置在另一个控件中,那么您将不得不在该新控件中进行搜索,或者,如果您仍想使用 kataSistimataFormView 作为父控件,则可能必须使用递归搜索.

谷歌的“findcontrol recursive”有一些很好的例子,你可以直接剪切和粘贴。

【讨论】:

  • 我猜这意味着正如我所怀疑的那样,因为它们现在进入了 Html 表的行和列,我必须获取表然后搜索它,如果它不是一些噪音的话:)
【解决方案2】:

这似乎是由于各种模板(插入、编辑、项目)上的相同命名 ID 造成的。即使编译器支持这一点,当您稍后以编程方式使用它们时也会出现问题。

谢谢大家。

【讨论】:

    【解决方案3】:

    你有想过这个吗?如果您知道 ID,则可以使用此递归函数:

    private Control FindControlRecursive(Control root, string id) 
    { 
        if (root.ID == id)
        { 
            return root; 
        } 
    
        foreach (Control c in root.Controls) 
        { 
            Control t = FindControlRecursive(c, id); 
            if (t != null) 
            { 
                return t; 
            } 
        } 
    
        return null; 
    } 
    

    在这里找到: http://www.codinghorror.com/blog/2005/06/recursive-pagefindcontrol.html

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多