6

i resolve a client id of a Repeater Item control, and i want to use it in other command, how cant i get the control by his client id?

TextBox TB = FindControl...?

GôTô
  • 7,974
  • 3
  • 32
  • 43
Roy Amir
  • 457
  • 1
  • 6
  • 15

6 Answers6

7

Are you trying to find the textbox that resides inside the repeater? If so, you could use the method below which searches based on the ID of the control - you could modify it to check based on the clientID of the control instead.

  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;
    }
Madeleine
  • 2,152
  • 2
  • 24
  • 35
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;
    }
hoaz
  • 9,883
  • 4
  • 42
  • 53
0

Instead of looping all controls in the entire control tree you could split it and go from the group up one control at the time:

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

Note: this function uses ClientIDSeparator its a protected property that is defined in Control class, so this method has to be used in something that inherits Control.

Peter
  • 37,042
  • 39
  • 142
  • 198
0

The shortest code is here:

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;
}
Firas Nizam
  • 1,392
  • 12
  • 7
0
<%= Control.ClientID %>
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0

Do you have access to the specific RepeaterItem (like you would in the ItemDataBound event handler)?

If so, you can do repeaterItem.FindControl("YourControlId") to get the child control.

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50