I encountered a problem during development today, which is roughly like this: my page needs an Excel export function, so I used the rendercontrol () of the gridview control () when running, grvzb must be placed in a form tag with runat = server." . So I quickly checked the HTML code and found that my <form> tag is marked with runat = "server", but the declaration of the <form> tag is a little farther away from the declaration of the gridview. No way. Fix any errors. So I opened the familiar Google. When an error message is entered, it is found that most of the search results are marked by "runat = server" on the <form> tag, which is obviously inconsistent with mine. Fortunately, a solution is found as follows:
Override the verifyrenderinginserverform method of the page base class on the page
Public override void verifyrenderinginserverform (Control)
{
// Confirms that an htmlform control is rendered
}
Msdn explains this method as follows:
The control must be located in the <form runat = Server> flag. You can call this method before rendering to Display error messages when the control is placed outside the tag. The control that sends back or relies on the registered script block should call this method in the override of the control. Render method. You can override this method for different pages that render server form elements to cause exceptions under different conditions.
If the server controls for sending back or using client scripts are not included in the htmlform Server Control (<form runat = "server">) Tag, they will not work properly. These controls can call this method during rendering to provide clear error information when they are not included in the htmlform control.
This method is usually called when the render method is rewritten for any type of input tag during custom server control development. This is especially important when the input control calls getpostbackeventreference or sends a client script. This call is not required for composite server controls.
Note: I saw another solution on the internet today. This solution is better, so we don't have to rewrite the verifyrenderinginserverform method. The Code is as follows:
Code
Stringbuilder sb = new stringbuilder ();
Stringwriter Sw = new stringwriter (SB );
Htmltextwriter HTW = new htmltextwriter (SW );
Page page = new page ();
Htmlform form = new htmlform ();
Gridview1.enableviewstate = false;
// Deshabilitar la validaci ón de Eventos, s ólo Asp.net 2
Page. enableeventvalidation = false;
// Realiza las inicializaciones de la instancia de la clase page que requieran los Dise n adores rad.
Page. designerinitialize ();
Page. Controls. Add (form );
Form. Controls. Add (gridview1 );
Page. rendercontrol (HTW );
Response. Clear ();
Response. Buffer = true;
Response. contenttype = "application/vnd. MS-excel ";
Response. addheader ("content-disposition", "attachment?filename=data.xls ");
Response. charset = "UTF-8 ";
Response. contentencoding = encoding. default;
Response. Write (sb. tostring ());
Response. End ();