Control must be in server form

Last post 02-07-2008 8:18 AM by willb75. 3 replies.

Sort Posts:

  • Control must be in server form

    02-06-2008, 5:39 AM
    • Member
      2 point Member
    • willb75
    • Member since 02-06-2008, 5:18 AM
    • Posts 3

    I was just playing about with the treeview control adapter, having used the default template project provided from the site. I just stuck a treeview on the default.aspx page, which doesn't have a server form (which I realised was the problem), and then bound it to a sitemap. The control seemed to render, but I got javascript errors when I tried clicking on any of the buttons to expand and collapse the nodes. I couldn't figure out what I'd done wrong, but then I removed the mapping to the adapter in the browsers file and got the following error:

    Control 'TreeView1' of type 'TreeView' must be placed inside a form tag with runat=server.

    The problem was obvious from this message, but the adapted rendering had failed to throw this error. Fortunately I've got a copy of .NET Reflector, so I could see inside the RenderContents method of the System.Web.UI.WebControls.TreeView class, and I noticed the following call:

    protected internal override void RenderContents(HtmlTextWriter writer)
    {
        base.RenderContents(writer);
        if (this.Page != null)
        {
            this.Page.VerifyRenderingInServerForm(this);
        }
    ...

    So I thought I'd try my luck at putting that code into the RenderContents method of the adapter, as follows:

    protected override void RenderContents(HtmlTextWriter writer)
    {
        TreeView treeView = Control as TreeView;
       
    if
    (this.Page != null)
        {
            this.Page.VerifyRenderingInServerForm(treeView);
        }
    .....

    And guess what, it works! I had a similar problem with another control, and I was scratching my head trying to figure out what was wrong, when I found that there are subtle differences like this which could cause a lot of development time to be lost. After all, the only difference between the tailor-made and adapted rendering should be the mark-up itself...

    Could anybody have a look at putting this code into the next release?

    cheers

  • Re: Control must be in server form

    02-06-2008, 7:18 AM
    • All-Star
      22,266 point All-Star
    • vik20000in
    • Member since 12-30-2005, 6:02 AM
    • Kolkata
    • Posts 3,464

    that looks like a pretty good finding. I will look at this myself later. :-)

    Vikram
    www.vikramlakhotia.com


    Please mark the answer if it helped you
  • Re: Control must be in server form

    02-06-2008, 1:57 PM
    • Member
      435 point Member
    • bdemarzo
    • Member since 07-02-2002, 8:05 AM
    • New York
    • Posts 168

    Can you confirm whether other CSS adapted controls have the same potential issue?

     

  • Re: Control must be in server form

    02-07-2008, 8:18 AM
    • Member
      2 point Member
    • willb75
    • Member since 02-06-2008, 5:18 AM
    • Posts 3

    I'm currently working on a project which will probably require other controls. I haven't used the others to be able to encounter the issue, but the easiest way to test each one is to instantiate and populate a web control outside a server form, and then try commenting out the adapter mapping in the browsers file. If you get the error with the default asp.net rendering, but not the css adapter rendering, then the css adapter rendering is wrong. You'll need to add the following, or equivalent code, to the RenderContents method:

    if (this.Page != null)
    {
        this.Page.VerifyRenderingInServerForm(control);
    }

    I think there was another related but different issue I came across with the CSS adapters, where the ASP.NET form validation was failing, because there it was missing a call to a routine in the framework - I'll post that too when I get a moment.

Page 1 of 1 (4 items)