How to handle eventvalidation exceptions in Asp.net 2.0

Source: Internet
Author: User

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 );

}

}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.