How to avoid adding the form tag with server controls?

Last post 10-26-2008 12:05 PM by Mikhail Arkhipov (MSFT). 6 replies.

Sort Posts:

  • How to avoid adding the form tag with server controls?

    10-19-2008, 2:00 PM
    • Member
      point Member
    • LLook
    • Member since 09-14-2008, 3:56 PM
    • CZ
    • Posts 5
    Hello, I've got this problem: In design mode, when I drag any server control into a page from the toolbar, and that page doesn't contain any server form tag, then the designer creates one. Even if it's not necessary (some controls don't use postback). Is there any possibility to avoid this behavior, at least for my own controls? Some attribute that I didn't find?
  • Re: How to avoid adding the form tag with server controls?

    10-19-2008, 3:40 PM
    • All-Star
      36,316 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 8:51 AM
    • Lincoln, England
    • Posts 5,847

    No I dont think its possible, the whole system is based on this method.

    If you need more control over your form element you might want to look into mvc.

    Also there are some ways to add an extra form into a asp.net webforms page.

     

    Why dont you want the <form> tag in there?

  • Re: How to avoid adding the form tag with server controls?

    10-19-2008, 6:35 PM
    • Member
      point Member
    • LLook
    • Member since 09-14-2008, 3:56 PM
    • CZ
    • Posts 5

    Not the whole system, only most of built-in controls. The form is needed only if you're using the ViewState/ControlState and postback. 

    Example of a simple control that doesn't need the form: 

    using System;
    using System.Web;
    using System.Web.UI;
    
    namespace WebSite3
    {
        [ToolboxData("<{0}:HelloWorld runat=\"server\" />")]
        public class HelloWorld : Control
        {
            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write("Hello World");
            }
        }
    }
    
    
       
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
    <%@ Register TagPrefix="hello" Namespace="WebSite3" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <hello:HelloWorld runat="server" />
    </body>
    </html>

    No problem, it works perfectly.

    The problem is only in the design view in VWD (and possibly in other editions of VS) - if I grab such control from the toolbar, it creates also that useless <form> tag and then I have to delete it manually (because I don't want that garbage in my HTML output and because sometimes I place other forms into my pages).

  • Re: How to avoid adding the form tag with server controls?

    10-24-2008, 11:31 AM
    • Member
      point Member
    • LLook
    • Member since 09-14-2008, 3:56 PM
    • CZ
    • Posts 5
    From now I am cosidering this as a Visual Studio weakness and I have to deal with it. Possibilities are these: 1) I can stop using Design View. 2) I can always delete the tag manually. 3) I can use some sort of pre/post-processing. 4) I can define a control adapter for the System.Web.UI.HtmlControls.HtmlForm. One worse than the other...
  • Re: How to avoid adding the form tag with server controls?

    10-24-2008, 11:40 AM

    Unfortunately, VS does not know if particular control requires form or not. Since most controls do and it may be difficult for users to figure out why page does not work when form is missing, designer default to a most common case.

    Thanks

    ------------------------------------------------------------

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: How to avoid adding the form tag with server controls?

    10-25-2008, 5:27 AM
    • Member
      point Member
    • LLook
    • Member since 09-14-2008, 3:56 PM
    • CZ
    • Posts 5

    Yes, I see. I just hoped that there is something like a design time attribute for this, but it isn't.

    I found that the requirement of the form is made by calling the Page.VerifyRenderingInServerForm(control) method. This method is called only from some form controls (Button, DropDownLis, FileUpload, CheckBox, ImageButton, LinkButton, ListBox, Login, LoginStatus, TextBox), and of course controls that are compound of these. There is a lot of built-in controls, that don't need a form, for example data sources (EntityDataSource etc.) or Content + ContentPlaceholder etc.

    What I found interesting is, that VS behaves this way only in the design view. If I drag controls into the source view, then no server form is created.

    Finally I will be probably using the adaptive rendering. Something like this: 

    <browsers>
      <browser refID="Default">
        <controlAdapters markupTextWriterType="System.Web.UI.HtmlTextWriter">
          <adapter
              controlType="System.Web.UI.HtmlControls.HtmlForm"
              adapterType="FormAdapter"
              />
        </controlAdapters>
      </browser>
    </browsers>
      
    using System.Web.UI.Adapters;
    
    public class FormAdapter : ControlAdapter
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            foreach (System.Web.UI.Control c in Control.Controls)
            {
                c.RenderControl(writer);
            }
        }
    }
    
     
  • Re: How to avoid adding the form tag with server controls?

    10-26-2008, 12:05 PM

    Unfortunately, server code in the control does not run in Design view, i.e. Page methods are not called. Only code that is relevant to design time experience is running. DV is not the same environment as ASP.NET process running in an actual Web server. 

    Source view is different, it operates strictly on text and does not make assumptions if your page will render or not. You can do whatever you want in Source, it will not change your markup. For example, DV will not product broken HTML while you are free to type one in Source. Also, Source does not instantiate controls while designer does, hence designer has to be stricter so it is able to create a control instance.

    Thanks

    ------------------------------------------------------------

    This posting is provided "AS IS" with no warranties, and confers no rights.
Page 1 of 1 (7 items)