2
votes

Im working with asp.net framework 4.0 and I have this code:

form id="form1" runat="server" method="get" action="Profile.aspx"

 // some code

asp:Button runat="server" ID="SubmitButton" Text="Submit"

Each time i click the submit button i get this error:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

any idea how to fix it ???

2
Are you running this in a web farm or cluster? - Oded
Are any of the preconditions mentioned here blogs.msdn.com/b/tom/archive/2008/03/14/… met? - SWeko
@Oded.. no @Sweko : i checked the link before and it didnt help me - Grace
Are you changing any form values through javascript? In particular the __viewstate hidden form field? - Oded
Do you by any chance submit to a page (Profile.aspx) different than the page the form is on? - Stilgar

2 Answers

2
votes

This is caused by cross-page POST (i.e. you are submitting the ViewState of the first page to the second). You can add PostBackUrl to the button like this:

<asp:Button runat="server" ID="SubmitButton" Text="Submit" PostBackUrl="~/WebForm2.aspx" />

Alternatively you can handle the click event of the button in the first page, move some of the logic in this handler and do a Response.Redirect (i.e. GET request) to the second page. The right solution depends on your particular case.

1
votes

If you want to send post back to your current page, remove in form tag method="get" action="Profile.aspx" attributes. And handle in codebehind post data from your page.

If your want to send data you another page like Profile.aspx, use PostBackUrl attribute of the button control, like Stilgar wrote for you. And then in Profile.aspx codebehind to get access to control from your current page use somethink like this:

If(Page.PreviousPage != null)
{
    var textBox = Page.PreviousPage.FindControl("ControlID") as TextBox;
    if(textBox != null)
    {
        //Use your logic here
    }
}

Hope it will be helpful for you!

Best regards, Dima.