Tuesday, December 25, 2012

A page can have only one server-side Form tag Error

Scenario:-
            I  have an aspx page and inside that I have a button that when the user clicks on that button,  generate the word document og the page.I have written for the button's click function.

Platform: Asp.net,c# and sharepoint

Exception Details:-
               I am able to generate the document but after the page is executed, the following error is displayed.
I am absolutely there is only form within the page because if the implementation of the above function is empty,I do not get any error. The webpage loads fine and clicking the button does nothing.

"A page can have only one server-side Form tag."
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: A page can have only one server-side Form tag.



This kind of error occuered on button click event on page when you are making function call  this.Page.RenderControl(hw);

Just follow the below steps it will defenately resolved this( A page can have only one server-side Form tag.
) exception.


1) create boolean variable like below and make it false.

    private bool test = false;

2) use that variable(test) in button click event and make it true 

    protected void test_click(object sender, EventArgs e)  
       { this.test = true; }

3) Now third imprtant step is to override render method and add your code(previously written in button   click function) in render method.
  
   protected override void Render(HtmlTextWriter writer)   
     {   if (this.test)      {
       //add your code here and remove code from button click function
      }
    }