Proper Way To Find The Form

code, asp.net 0 comments suggest edit

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.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

7 responses

  1. Avatar for Kamran Shaid
    Kamran Shaid June 13th, 2006

    What Will be its use ?

  2. Avatar for Daniel
    Daniel June 13th, 2006

    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...

  3. Avatar for Haacked
    Haacked June 13th, 2006

    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?

  4. Avatar for Scott
    Scott June 13th, 2006

    Would a more useful, generalized method be
    public string GetControlId(Type controlType).
    or
    public static string GetControlId(Type controlType, Control page)

  5. Avatar for Haacked
    Haacked June 14th, 2006

    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)

  6. Avatar for Christopher Steen
    Christopher Steen June 18th, 2006

    [Cool Tool]Unlocker : Unlock Files/Folders without going

    nuts [Via: RoyOsherove ]

    Awesome Firefox...

  7. Avatar for you've been HAACKED
    you've been HAACKED March 4th, 2007

    Replacing Recursion With a Stack