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.