【问题标题】:find a control in current page在当前页面中查找控件
【发布时间】:2013-05-09 20:44:10
【问题描述】:

您好,我的问题是我似乎无法从当前页面找到控件。我的页面类有以下代码:

        <div class="meeting_body_actions"> 
                <efv:ViewMeetingActions ID="ViewMeetingActions" runat="server" />
        </div>

我的控制有:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewMeetingActions.ascx.cs" Inherits="EFV.Controls.ViewMeetingActions" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

Telerik:RadListBox runat="server"  CssClass="RadListBox" ID="listbox_action_member" Width="125" Height="200px" Skin="Telerik" OnTransferring="ActionListBoxViewer_Transferring" OnDeleting="ActionListBoxViewer_Deleting" >
         <ButtonSettings AreaHeight="30" Position="Bottom" HorizontalAlign="Center" />  
        <HeaderTemplate>               
        </HeaderTemplate>
        <Items>
        </Items>
      <FooterTemplate>
          <asp:DropDownList runat="server" ID="action_member_dropdown"  Height="22" Width="125"  ></asp:DropDownList>
        </FooterTemplate>
    </telerik:RadListBox

从另一个控件我需要在“action_member_dropdown”中输入信息;

  Control control = this.Page.FindControl("ViewMeetingActions");
  -> doesnt work

        Page page = HttpContext.Current.Handler as Page;
        Control ViewMeetingActions = page.FindControl("ViewMeetingActions");
        -> didnt work as well

Page test = this.Parent.Page;
-> no succes

如果我问页面我有多少控件,它说我有 1 个控件,我添加的控件超过 5 个。

简而言之,我如何从另一个控件调用同一页面中的控件?

【问题讨论】:

    标签: c# telerik controls


    【解决方案1】:

    如果一个控件嵌套在其他控件中,则需要递归查找。

    这是一个辅助方法。它递归地搜索控件。

    辅助方法

    public static Control FindControlRecursive(Control root, string id)
    {
       if (root.ID == id) 
         return root;
    
       return root.Controls.Cast<Control>()
          .Select(c => FindControlRecursive(c, id))
          .FirstOrDefault(c => c != null);
    }
    

    用法

    var myControl =  (MyControl)FindControlRecursive(Page, "ViewMeetingActions"); 
    

    【讨论】:

      【解决方案2】:

      首先,递归循环浏览页面上的控件。使用以下帮助类:

      using System.Web.UI;
      
      public class ReflectionHelper
      {
      
      /// <summary>
      /// Check Control for match on ID and recursively check all Children for match on ID.
      /// </summary>
      /// <param name="ParentControl"></param>
      /// <param name="ControlId"></param>
      /// <returns>Control if found, null if not found</returns>
      /// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
      public static Control FindControlRecursive(Control ParentControl, string ControlId)
      {
          if (ParentControl.ID == ControlId) {
              return ParentControl;
          }
      
          foreach (Control Ctl in ParentControl.Controls) {
              Control FoundCtl = FindControlRecursive(Ctl, ControlId);
              if ((FoundCtl != null)) {
                  return FoundCtl;
              }
          }
          return null;
      }
      
      /// <summary>
      /// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to Invoke() Control method.
      /// </summary>
      /// <param name="ParentControl"></param>
      /// <param name="ControlId"></param>
      /// <param name="MethodName"></param>
      /// <param name="parameters"></param>
      /// <returns>bool true if executed, bool false if error or not executed</returns>
      /// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
      public static bool FindControlRecursiveAndInvokeMethod(Control ParentControl, string ControlId, string MethodName, object[] parameters)
      {
          var ctrl = FindControlRecursive(ParentControl, ControlId);
      
          if (ctrl != null)
          {
              try
              {
                  MethodInfo[] ctrlMethods = ctrl.GetType().GetMethods();
                  foreach (MethodInfo method in ctrlMethods)
                  {
                      if (method.Name == MethodName)
                      {
                          method.Invoke(ctrl, parameters);
                          return true;
                      }
                  }
                  //return false;
              }
              catch (System.Exception)
              {
                  //return false;
              }
          }
          else
          {
              //return false;
          }
      
          return false;
      }
      
      /// <summary>
      /// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to set SetValue() on Control property.
      /// </summary>
      /// <param name="ParentControl"></param>
      /// <param name="ControlId"></param>
      /// <param name="PropertyName"></param>
      /// <param name="value"></param>
      /// <returns>bool true if executed, bool false if error or not executed</returns>
      /// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
      public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, string value)
      {
          var ctrl = FindControlRecursive(ParentControl, ControlId);
      
          if (ctrl != null)
          {
              try
              {
                  PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
                  foreach (PropertyInfo property in ctrlProperties)
                  {
                      if (property.Name == PropertyName)
                      {
                          property.SetValue(ctrl, value, new object[0]);
                          return true;
                      }
                  }
                  //return false;
              }
              catch (System.Exception)
              {
                  //return false;
              }
          }
          else
          {
              //return false;
          }
      
          return false;
      }
      
      /// <summary>
      /// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to set SetValue() on Control property.
      /// </summary>
      /// <param name="ParentControl"></param>
      /// <param name="ControlId"></param>
      /// <param name="PropertyName"></param>
      /// <param name="value"></param>
      /// <returns>bool true if executed, bool false if error or not executed</returns>
      /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
      public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, int value)
      {
          var ctrl = FindControlRecursive(ParentControl, ControlId);
      
          if (ctrl != null)
          {
              try
              {
                  PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
                  foreach (PropertyInfo property in ctrlProperties)
                  {
                      if (property.Name == PropertyName)
                      {
                          property.SetValue(ctrl, value, new object[0]);
                          return true;
                      }
                  }
                  //return false;
              }
              catch (System.Exception)
              {
                  //return false;
              }
          }
          else
          {
              //return false;
          }
      
          return false;
      }
      
      }
      

      其次,使用类获取Control Ref:

      Control ctrlActionMemberDropdown = ReflectionHelper.FindControlRecursive(this.Page, "action_member_dropdown");
      

      三、将行插入到DropDownList控件中:

      ctrlActionMemberDropdown.Items.Insert(0, "<-- Select -->");
      

      谢谢,

      【讨论】:

        【解决方案3】:

        Page.Controls 为您提供了控件层次结构中最顶层控件的集合。 WebForm 本身就是一个控件,并且包含许多其他控件。您需要遍历此层次结构才能查看整个控件集合。

        FindControl 方法应该找到您正在寻找的控件。您能否与我们分享更多代码来演示该问题?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-02
          • 1970-01-01
          相关资源
          最近更新 更多