2010-10-14 11 views
5

ich auflösen eine Client-ID eines Repeater Item-Steuerelement, und ich möchte es in anderen Befehl verwenden, wie kann ich die Kontrolle von seinem Client-ID bekommen?bekomme Kontrolle durch clientID

TextBox TB = FindControl ...?

Antwort

7

Versuchen Sie, die Textbox zu finden, die sich im Repeater befindet? Wenn dies der Fall ist, können Sie die Methode verwenden, nach der die Suche anhand der ID des Steuerelements ausgeführt wird. Sie können sie stattdessen so ändern, dass sie anhand der clientID des Steuerelements überprüft wird.

public static System.Web.UI.Control FindControlIterative(System.Web.UI.Control root, string id) 
    { 
     System.Web.UI.Control ctl = root; 
     var ctls = new LinkedList<System.Web.UI.Control>(); 

     while (ctl != null) 
     { 
      if (ctl.ID == id) 
       return ctl; 
      foreach (System.Web.UI.Control child in ctl.Controls) 
      { 
       if (child.ID == id) 
        return child; 
       if (child.HasControls()) 
        ctls.AddLast(child); 
      } 
      if (ctls.First != null) 
      { 
       ctl = ctls.First.Value; 
       ctls.Remove(ctl); 
      } 
      else return null; 
     } 
     return null; 
    } 
+1

Vielen Dank! Es hat mir sehr geholfen. –

0
<%= Control.ClientID %> 
0

Haben Sie Zugriff auf das spezifische RepeaterItem (wie im ItemDataBound-Ereignishandler)?

Wenn ja, können Sie repeaterItem.FindControl("YourControlId") tun, um die untergeordnete Kontrolle zu bekommen.

0
public static System.Web.UI.Control GetControlIterativeClientID(System.Web.UI.Control root, string id) 
    { 
     System.Web.UI.Control ctl = root; 
     var ctls = new LinkedList<System.Web.UI.Control>(); 
     if (root != null) 
     { 
      if (ctl.ID == id) 
       return ctl; 
      foreach (System.Web.UI.Control child in ctl.Controls) 
      { 
       if (child.ID == id) 
        return child; 
       if (child.HasControls()) 
        GetControlIterativeClientID(child, id);       
      }     
     } 
     return null; 
    } 
0

Anstatt alle Kontrollen in dem gesamten Steuerbaum von looping Sie es geteilt konnten und zu der Zeit aus der Gruppe, bis eine Kontrolle gehen:

public Control GetControlByClientId(string clientId) 
{ 
    Queue<string> clientIds = new Queue<string>(clientId.Split(ClientIDSeparator)); 
    Control root = this.Page; 
    string subControlId = null; 
    while (clientIds.Count > 0) 
    { 
     if (subControlId == null) 
     { 
      subControlId = clientIds.Dequeue(); 
     } 
     else 
     { 
      subControlId += ClientIDSeparator + clientIds.Dequeue(); 
     } 
     Control subControl = root.FindControl(subControlId); 
     if (subControl != null) 
     { 
      root = subControl; 
      subControlId = null; 
     } 
    } 
    if (root.ClientID == clientId) 
    { 
     return root; 
    } 
    else 
    { 
     throw new ArgumentOutOfRangeException(); 
    } 
} 

Hinweis: Diese Funktion verwendet ClientIDSeparator seine geschütztes Eigentum das ist in Control Klasse definiert, so dass diese Methode in etwas verwendet werden muss, das Control erbt.

0

Der kürzeste Code ist hier:

private Control getControl(Control root, string pClientID) 
{ 
    if (root.ClientID == pClientID) 
     return root; 
    foreach (Control c in root.Controls) 
     using (Control subc= getControl(c, pClientID)) 
      if (subc != null) 
       return subc; 
    return null; 
} 
Verwandte Themen