2011-09-13 10 views

答えて

3

あなたはあなたのコントロールを再帰する必要があります(C#コード)

public static Control FindControl(Control parentControl, string fieldName) 
    { 
     if (parentControl != null && parentControl.HasControls()) 
     { 
      Control c = parentControl.FindControl(fieldName); 
      if (c != null) 
      { 
       return c; 
      } 

      // if arrived here, then not found on this level, so search deeper 

      // loop through collection 
      foreach (Control ctrl in parentControl.Controls) 
      { 
       // any child controls? 
       if (ctrl.HasControls()) 
       { 
        // try and find there 
        Control c2 = FindControl(ctrl, fieldName); 
        if (c2 != null) 
        { 
         return c2; // found it! 
        } 
       } 
      } 
     } 

     return null; // found nothing (in this branch) 
    } 
+0

thanxこれはfieldNameパラメーターの[ClientID]と[ID]の両方で機能します。 – TroyS

0

これは私が過去に使用してきた拡張メソッドです。拡張メソッドとして使用すると、コードが少し表現力豊かになることがわかりましたが、それは単に好みです。

/// <summary> 
/// Extension method that will recursively search the control's children for a control with the given ID. 
/// </summary> 
/// <param name="parent">The control who's children should be searched</param> 
/// <param name="controlID">The ID of the control to find</param> 
/// <returns></returns> 
public static Control FindControlRecursive(this Control parent, string controlID) 
{ 
    if (!String.IsNullOrEmpty(parent.ClientID) && parent.ClientID.Equals(controlID)) return parent; 

    System.Web.UI.Control control = null; 
    foreach (System.Web.UI.Control c in parent.Controls) 
    { 
     control = c.FindControlRecursive(controlID); 
     if (control != null) 
      break; 
    } 
    return control; 
} 
+0

@ jamar777 thanx、これはconrol.IDを渡すが、control.ClientIdは渡していない場合に機能します。私はuser_controlのインスタンス(同じuser_controlのいくつかのインスタンス)の子を見つけようとしているので、ClientIDを使用する必要があります。 – TroyS

+0

control.ClientIDと比較するコードをjsutで更新できませんか? – jmar777

+0

更新のためのID – jmar777

関連する問題