2010-10-14 12 views
5

i rozwiązać identyfikator klienta kontroli elementu typu Repeater, i chcę go użyć w innym poleceniu, jak mogę uzyskać kontrolę przez jego identyfikator klienta?uzyskać kontrolę przez clientID

TextBox TB = FindControl ...?

Odpowiedz

7

Próbujesz znaleźć pole tekstowe znajdujące się w repeterze? Jeśli tak, możesz użyć metody, poniżej której wyszukiwania będą oparte na identyfikatorze kontrolki - możesz go zmodyfikować, by sprawdzać na podstawie identyfikatora klienta kontrolki.

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

Thanks a lot! Bardzo mi to pomogło. –

0
<%= Control.ClientID %> 
0

Czy masz dostęp do określonego RepeaterItem (jak w przypadku obsługi zdarzeń ItemDataBound)?

Jeśli tak, możesz zrobić repeaterItem.FindControl("YourControlId"), aby uzyskać kontrolę nad dzieckiem.

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

Zamiast pętli wszystkie kontrole w całym drzewie sterowania można je rozdzielić i iść z grupy maksymalnie jednej kontroli w tym czasie:

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(); 
    } 
} 

Uwaga: Funkcja ta wykorzystuje ClientIDSeparator jego chronionej własności który jest zdefiniowany w klasie Control, więc ta metoda musi być używana w czymś, co dziedziczy Control.

0

najkrótszy kod jest tutaj:

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; 
} 
Powiązane problemy