【问题标题】:Using Page.FindControl() to get an instance of a (third party) control keeps returning null使用 Page.FindControl() 获取(第三方)控件的实例不断返回 null
【发布时间】:2011-07-28 23:31:00
【问题描述】:

我有一个 ASP.NET 站点,在一个页面上,有几个控件(第三方 - Telerik)。

如果我使用 Page.FindControl(),并传入控件的 ID(拼写正确),这总是返回 null。为什么?

这是在一个 .aspx 页面上,并且控件本身不是一个控件等。不记得是否有母版页,或者,因此假设任何可能的答案是和否。

如何以编程方式获取控件的实例?

谢谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    如果你想使用FindControl,你需要引用控件的NamignContainer。如果您不知道(或者您没有参考)它,您必须递归循环控制树:

    例如(作为扩展名):

    namespace ExtensionMethods
    {
        public static class ControlExtensions
        {
            public static Control FindControlRecursive(Control rootControl, string controlID)
            {
               if (rootControl.ID == controlID) return rootControl;
    
               foreach (Control controlToSearch in rootControl.Controls)
               {
                  Control controlToReturn = FindControlRecursive(controlToSearch, controlID);
                  if (controlToReturn != null) return controlToReturn;
               }
               return null;
            }
        }
    }
    

    这样使用:

     using ExtensionMethods;
     //.....
     Control ctrl = this.FindControlRecursive("myControlID");
    

    但如果您知道 NamingContainer,最好使用 FindControl,因为:

    1. 更快
    2. 更清晰
    3. 不易出错:控件的 ID 只需要在其 NamingContainer 内是唯一的,因此您可能会使用递归方法得到错误的控件(考虑带有行的 GridView 和 TemplateField 内的控件)李>
    4. 您将了解更多 ASP.NET 页面的工作原理和控件的链接方式

    更多信息:MSDN How to: Access Server Controls by ID

    【讨论】:

      【解决方案2】:

      您尝试查找的控件很可能嵌套在控件树的较低位置。您可以使用以下代码递归遍历树并找到您的控件,无论它嵌套在控件层次结构中多远或多近

       Control c =  FindControl<Control>(Page,"controlIDStringLiteral");
      
       public static T FindControl<T>(ControlCollection controls, string controlId) where T : Control
          {
              if (controls == null)
              {
                  throw new ArgumentException("controls null");
              }
      
              if (string.IsNullOrEmpty(controlId))
              {
                  throw new ArgumentException("controlId is null or empty");
              }
      
              if (controls.Count < 1)
              {
                  return null;
              }
      
              Control retval;
              foreach (Control control in controls)
              {
                  if (control.ID == controlId)
                  {
                      return control as T;
                  }
              }
      
              foreach (Control control in controls)
              {
                  retval = FindControl<T>(control, controlId);
                  if (retval != null)
                  {
                      return retval as T;
                  }
              }
              return null;
          }
      
      
          /// <summary>
          /// </summary>
          /// <typeparam name="T"></typeparam>
          /// <param name="control"></param>
          /// <param name="controlId"></param>
          /// <returns></returns>
          public static T FindControl<T>(Control control, string controlId) where T : Control
          {
              if (control == null)
              {
                  throw new ArgumentException("control null");
              }
      
              if (control.Controls == null)
              {
                  return null;
              }
      
              if (control.Controls.Count < 1)
              {
                  return null;
              }
      
              Control retval;
              retval = control.FindControl(controlId);
              if (retval != null)
              {
                  return retval as T;
              }
      
              foreach (Control childControl in control.Controls)
              {
                  retval = FindControl<T>(childControl, controlId);
                  if (retval != null)
                  {
                      return retval as T;
                  }
              }
      
              return null;
          }
      

      【讨论】:

      • 谢谢,明天补上。如果控件是第三方,这有关系吗?
      • 不,如果控件是第 3 方控件,这无关紧要,因为所有控件都将从 WebControl 或 Control 继承。
      猜你喜欢
      • 2014-03-06
      • 2011-09-06
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2018-11-13
      • 2017-01-28
      相关资源
      最近更新 更多