Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# find controls and control reference

How do I reference controls in c# (runtime) without an ID?

For example, I have a page with one table in it. I will be loading this page recursively using xmlhttp so I cannot refer to the table using it's id. Is there something to the effect of this.Page.Controls[2]? or Controls["Tables"][0]?

I tried using the name this.Controls.Find("MyTableName", true); but need a reference to the library System.Windows.Forms (I think) but don't know how to add it as the 'using System.' intellisense can't see it.

I looped through all the controls in this.Controls but 'system.ui.web.control' does not contain a definition for '.Name' so I can only search for ID.

I am new to this and I am sure the solutions are infuriatingly simple.
Thanks in advance

like image 613
Praesagus Avatar asked Dec 17 '22 06:12

Praesagus


1 Answers

To find a control on your page, it has to be a server control, like this:

<asp:Table runat="server">
   ...
</asp:Table>

Regular HTML on the page are not "controls", they are just text that gets sent to the browser. Server controls, on the other hand, are actual .NET classes which can interact with the codebehind.

You can still get a handle to this control without its ID by searching the Controls collection of its container, or by recursively searching the page. Let's start with recursively searching the Controls collection:

The Controls collection only refers to immediate child controls of a given control. Or immediate child controls of a Page. Those controls in turn have child controls of their own. It represents a tree in memory.

Here is a method to recurse down the tree from a given control and find the control by ID:

private Control FindControlRecursive(Control control, string id)
{
    Control returnControl = control.FindControl(id);
    if (returnControl == null)
    {
        foreach (Control child in control.Controls)
        {
            returnControl = child.FindControlRecursive(id);
            if (returnControl != null && returnControl.ID == id)
            {
                return returnControl;
            }
        }
    }
    return returnControl;
}

(Beyond the scope of this answer, this is better-done as an extension method).

To find a control by something other than its ID, you can search by type:

if(someControl is System.Web.UI.WebControls.Table)

Note that this is generally not a very good idea. It's not a great pattern if you end up having to search for controls this way - you should have an ID to your control, or you should have a reference to it already because it was created in code.

However, it is simple to modify the method to recursively search for a type:

private Control FindTable(Control startFrom)
{
    foreach(Control child in startFrom.Controls)
    {
        if(child is System.Web.UI.WebControls.Table)
        {
            return child;
        }
        else
        {
            return FindTable(child);
        }
    }
    return null;
}

You could also have a generic form of this method:

private Control FindControl<T>(Control startFrom)
{
    foreach(Control child in startFrom.Controls)
    {
        if(child.GetType().IsAssignableFrom(typeof(T)))
        {
            return child;
        }
        else
        {
            return FindControl<T>(child);
        }
    }
    return null;
}

You definitely don't want to include System.Windows.Forms, as that simply includes all the code for a WinForms application. That's why Visual Studio has not included it for you in a web project - you'll never need it. System.Web.UI has everything for web controls.

like image 180
Rex M Avatar answered Dec 19 '22 19:12

Rex M