开发者

get control by clientID

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?

TextB开发者_JAVA技巧ox TB = FindControl...?


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


<%= Control.ClientID %>


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.


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


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜