Proper Way To Find The Form

1040 EZ Form Today I ran across some code in a 3rd party open source library that used the following function in order to retrieve the form id.

public static string GetPageFormID(Control page)
{
    string id = null;

    foreach (Control con in page.Controls)
    {
        if (con is HtmlForm)
        {
            id = con.ClientID;
            break;
        }
    }
    return id;
}

Which gets called like so:

Control page = HttpContext.Current.Page;
string formID = GetPageFormID(page);

Unfortunately this didn’t work for me because I don’t have the form declared as a direct child of the page. Instead the page contains a user control which contains the form. This is a common scenario when using a MasterPage (in my case an ASP.NET 1.1 backported master page control). When looking for the form, the function should search recursively like so:

public static string GetPageFormID(Control page)
{
    string id = null;

    foreach (Control con in page.Controls)
    {
        if (con is HtmlForm)
        {
            return con.ClientID;
        }
        id = GetPageFormID(con);
        if(id != null)
            return id;
    }
    return id;
}

This will search the entire control hierarchy until it finds the HtmlForm. In the most common case, it will find it without having to recurse. But for crazy folks like me who always look for ways to be different, this will do the trick. Luckily this was an open source library I was using so I was able to fix the code and send the authors a patch.

What others have said

Requesting Gravatar... Kamran Shaid Jun 14, 2006 4:31 AM
# re: Proper Way To Find The Form
What Will be its use ?
Requesting Gravatar... Daniel Jun 14, 2006 6:26 AM
# re: Proper Way To Find The Form
We ran into a similar issue, but the problem was with multiple forms. A designer had sent us a dreamweaver page we used as a master page. Postbacks didn't work until we realized he'd put a second form for a little side-bar search. .NET was finding the wrong form to attach it's handlers. We were able to remove it in that case, but I could imagine some where it may not be possible...
Requesting Gravatar... Haacked Jun 14, 2006 9:01 AM
# re: Proper Way To Find The Form
Shaid, this code is useful when generating client-side javascript from the server.

Daniel, doesn't ASP.NET throw an exception when more than one server side form is on a page?
Requesting Gravatar... Scott Jun 14, 2006 10:58 AM
# re: Proper Way To Find The Form
Would a more useful, generalized method be

public string GetControlId(Type controlType).

or

public static string GetControlId(Type controlType, Control page)
Requesting Gravatar... Haacked Jun 14, 2006 1:06 PM
# re: Proper Way To Find The Form
Well sort of. With the HtmlForm and ASP.NET, you can generally be assured there is only one HtmlForm instance.

But there might be multiple instances of a given control type. So a method like:

public static string GetControlId(Type controlType, Control page)


Would only return the first id. Probably better to make it

public static string[] GetControlIds(Type controlType, Control page)
Requesting Gravatar... Christopher Steen Jun 18, 2006 11:25 PM
# Link Listing - June 18, 2006
[Cool Tool]Unlocker : Unlock Files/Folders without going
nuts [Via: RoyOsherove ]
Awesome Firefox...
Requesting Gravatar... you've been HAACKED Mar 04, 2007 9:33 PM
# Replacing Recursion With a Stack
Replacing Recursion With a Stack

What do you have to say?

(will show your gravatar)
Please add 5 and 4 and type the answer here: