Find a control in a webform

前端 未结 2 466
忘了有多久
忘了有多久 2020-12-03 08:13

I have a Web content form and need to access a control inside the content panel. I know of two ways to access the control:

  1. TextBox txt = (TextBox)Page.Co
相关标签:
2条回答
  • 2020-12-03 08:48

    The issue is that FindControl() does not traverse certain control children such as a templated control. If the control you are after lives in a template, it won't be found.

    So we added the following extension methods to deal with this. If you are not using 3.5 or want to avoid the extension methods, you could make a general purpose library out of these.

    You can now get the control you are after by coding:

    var button = Page.GetControl("MyButton") as Button;
    

    The extension methods do the recursive work for you. Hope this helps!

    public static IEnumerable<Control> Flatten(this ControlCollection controls)
    {
        List<Control> list = new List<Control>();
        controls.Traverse(c => list.Add(c));
        return list;
    }
    
    public static IEnumerable<Control> Flatten(this ControlCollection controls,     
        Func<Control, bool> predicate)
    {
        List<Control> list = new List<Control>();
        controls.Traverse(c => { if (predicate(c)) list.Add(c); });
        return list;
    }
    
    public static void Traverse(this ControlCollection controls, Action<Control> action)
    {
        foreach (Control control in controls)
        {
            action(control);
            if (control.HasControls())
            {
                control.Controls.Traverse(action);
            }
        }
    }
    
    public static Control GetControl(this Control control, string id)
    {
        return control.Controls.Flatten(c => c.ID == id).SingleOrDefault();
    }
    
    public static IEnumerable<Control> GetControls(this Control control)
    {
        return control.Controls.Flatten();
    }
    
    0 讨论(0)
  • 2020-12-03 09:03

    I would like to change your GetControls function to a generic one as follows:

    public static T GetControl<T>(this Control control, string id) where T:Control
    {
        var result = control.Controls.Flatten(c => (c.GetType().IsSubclassOf(typeof(T))) && (c.ID == id)).SingleOrDefault();
        if (result == null)
            return null;
        return result as T;
    }
    

    And Then,

    public static Control GetControl(this Control control, string id)
    {
        return control.GetControl<Control>(id);
    }
    

    This way, the caller would call something like:

    var button = Page.GetControl<Button>("MyButton");
    
    0 讨论(0)
提交回复
热议问题