Asp.net 2.0 adds an eventvalidation function. Added security. However, if you submit when the form is not fully loaded, you will encounter the following error:
Event validation is enabled using <pages enableeventvalidation = "true"/> in configuration or <% @ page enableeventvalidation = "true" %> in a page. for security purposes, this feature verifies that arguments to PostBack or callback events originate from the server control that originally rendered them. if the data is valid and expected, use the clientscriptmanager. registerforeventvalidation method in order to register the PostBack or callback data for validation.
In this regard, I provide two solutions to this problem:
1. Use JavaScript to hide the form during form load. After load is complete, it is displayed.
<Form ID= "Form1" Runat= "Server">
<Script Type= "Text/JavaScript" Language= "JavaScript">
VaRThisform = terraform Doc ument. Forms [0];
Thisform. style. Display ="None";
</Script>
<Div>
<ASP:Button ID= "Button1" Runat= "Server" Text= "Button" Onclick= "Button#click" />
</Div>
</Form>
<Script Type= "Text/JavaScript" Language= "JavaScript">
Thisform. style. Display ="";
</Script>
2 put _ in the C # code of the server __ Eventvalidation Move the tag to the start tag of the form, so that __ Eventvalidation loads before all the submit buttons to ensure that the browser does not submit __Eventvalidation form.
The specific method is to overload the page render method and use a regular expression to extract __ Eventvalidation tag, and insert it to the start tag of form.
Protected Override VoidRender (HtmltextwriterWriter)
{
StringbuilderSbrenderedhtml =New Stringbuilder();
StringwriterStrwriter =New Stringwriter(Sbrenderedhtml );
HtmltextwriterHtwriter =New Htmltextwriter(Strwriter );
Base. Render (htwriter );
StringRenderedhtml = sbrenderedhtml. tostring ();
RegExReg =New RegEx("^ (? <Formhead> .*? <\ S * Form (\ s +. *?>) |> )(? <Formbody> .*(? = <Div> .*? _ Eventvalidation .*? </Div> ))(? <Eventvalidation> <div> .*? _ Eventvalidation .*? </Div> )(? <Formtail>. *) $",Regexoptions. Singleline |Regexoptions. Ignorecase );
If(Reg. ismatch (renderedhtml ))
{
StringFixedhtml =RegEx. Replace (renderedhtml,"^ (? <Formhead> .*? <\ S * Form (\ s +. *?>) |> )(? <Formbody> .*(? = <Div> .*? _ Eventvalidation .*? </Div> ))(? <Eventvalidation> <div> .*? _ Eventvalidation .*? </Div> )(? <Formtail>. *) $","$ {Formhead }$ {eventvalidation }$ {formbody }$ {formtail }",Regexoptions. Singleline |Regexoptions. Ignorecase );
Writer. Write (fixedhtml );
}
Else
{
Writer. Write (renderedhtml );
}
}