Validation of viewstate MAC failed

  • Thread starter George Thompson
  • Start date
G

George Thompson

Hi,

I come from an ASP background, and I am currently working on a .NET
project, so please excuse me if this is a dumb question ...

I have written a dynamically generated menu that uses the following
structure (looping through for each menu item needing to be
displayed):

<tr>
<td class='menuclass' onmouseout='menu(this)'
onmouseover='menu_over(this)'>My Menu Text</td>
</tr>

the result is that the td, whose class contains a bg image, has a
simple mouse over image change. Now what I would like to do is have
it so that if you click anywhere on the td, it fires an onclick event
like this:

onclick="aspnetForm.action='mynewpagetogoto.aspx';
aspnetForm.submit();"

This works fine in an asp environment, however when I click on a td
with that onclick event in an asp.net environment I get this error:

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

as a result I am being forced to set an href link around the text
instead ... although this works, it would be ideal to have the entire
td clickable because the image change that happens does suggest that
it is and may be confusing for visitors.

Also, more importantly, I would like to understand the source of this
error, as it does surprise me that I cannot navigate away from this
page in this fashion.

Any help would be greatly appreciated!

Thanks!
 
M

Mark Fitzpatrick

Keep in mind, you aren't navigating away from the page, you are trying to
post to another page. That will cause problems. The ASP background really
hurts a lot of ASP.Net developers. First, you don't need to post to another
page. Instead, you can have it call a client-side script that performs it's
own postback to the page you are on, no reason to go to another page.
ASP.Net was designed so you can encapsulate all the logic within a single
page, no need to chain together a whole bunch of pages to do the same job.
You can do things like show steps in a process using a wizard control or a
series of panels, no needing to create a page for each step.

In this case, if you're showing information on a page and want to click on
something to show more, you can use panels. Put your table with images and
onclick events in one panel, then when you do a postback, hide the first
panel, load the information into the second panel.

You're running into a problem with something known as ViewState. ViewState
keeps information regarding the state of all the controls on a page. The
code you're using will get the ASP.Net page to attempt to post it's data to
another page. ASP.Net doesn't like that approach because the ViewState is
encrypted to avoid it being tampered with. The second page knows that the
ViewState it was sent isn't valid as it can't decrypt it so it throws an
error.

You could pass all the form fields that you need to submit to the second
page through a querystring during your onclick by changing the browser
location instead of submitting the form. that would let you send the data
without running into a viewstate issue as you would no longer be submitting
the form.

The biggest trick is to forget what you knew of ASP, ASP.Net is an entirely
new beast and is truly an event-driven model even though it shares a name
with classic ASP. It's the biggest trip-up for developers making the
transition is applying ASP knowledge to ASP.Net. Start looking at how simiar
tasks are handled and you'll get a much better feel of the platform and how
to avoid these types of issues.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 
M

Mark Rae [MVP]

I come from an ASP background
Any help would be greatly appreciated!

I can only reiterate what Mark has said.

ASP.NET is totally different from ASP in almost every way. The biggest
mistake that people coming from an ASP Classic background make is to imagine
that ASP.NET is the next version of ASP and, even worse, that VB.NET is the
next version of VBScript... In fact, there's a school of thought which says
that a good reason for choosing C# over VB.NET is precisely because you
won't bring any of the VBScript baggage with you. I'm assuming that you're
fairly familiar with JavaScript, so you won't find the C# syntax too
daunting...

I would strongly advise you to get a copy of this:
http://www.amazon.com/ASP-NET-All-R...4073238?ie=UTF8&s=books&qid=1193931423&sr=8-2
and work your way through it, otherwise you'll find ASP.NET a very steep
uphill struggle.

However, I can assure you that once you have grasped the basics, you'll
never want to go back to ASP Classic... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top