$("#aspnetForm").attr('action', newAction);

In SP2007 it worked fine, in SP2010 I get the following error in the SharePoint log:

System.Web.HttpException: Failed to load viewstate.

Can someone help me with this problem?

<script type="text/javascript" src="/_layouts/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
fields = init_fields();
// Where to go when cancel is clicked
goToWhenCanceled = '/test/English/YouCanceled.aspx';

// Edit the redirect on the cancel-button's

// Edit the form-action attribute to add the source=yourCustomRedirectPage
function setOnSubmitRedir(redirURL){
var action = $("#aspnetForm").attr('action');
var end = action.indexOf('&');
        newAction = action + "?Source=" + redirURL;
        newAction = action.substring(0,end) + "&Source=" + redirURL;

// Use this for adding a "static" redirect when the user submits the form
    var goToWhenSubmitted = '/test/English/ThankYou.aspx';

// Use this function to add a dynamic URL for the OnSubmit-redirect. This function is automatically executed before save item.
function PreSaveAction(){
// Pass a dynamic redirect URL to the function by setting it here,
// for example based on certain selections made in the form fields like this:

    var dynamicRedirect = '/surveys/Pages/ThankYou.aspx';

    // Call the function and set the redirect URL in the form-action attribute
    // This function must return true for the save item to happen
    return true;

function init_fields(){
  var res = {};
      if($(this).html().indexOf('FieldInternalName="')<0) return;
      var start = $(this).html().indexOf('FieldInternalName="')+19;
      var stopp = $(this).html().indexOf('FieldType="')-7;
      var nm = $(this).html().substring(start,stopp);
      res[nm] = this.parentNode;
  return res;
  • Do you have a test site for me to look at? I'd be more than happy to do some debugging on this. In the past though I've used the ASP.net button with commit actions, which also allows redirects.
    – TeckniX
    Commented Sep 6, 2011 at 22:12
  • It's an internal site.
    – Tony Stark
    Commented Sep 7, 2011 at 15:51
  • Why not use an asp form action instead?
    – TeckniX
    Commented Sep 12, 2011 at 13:10

3 Answers 3


Have you validated that the selector is valid with the 2010 DOM? I would be pretty surprised if something like that would just magically work in 2010 given the completely new DOM structure.

I'd use the Chrome or IE dev tools to validate that the selector is correct, and add some debug alerting so you know what portions of the script (if any) are executing. The Chrome dev tools console will output your script errors and at least give you a place to start.


I'm experiencing a similar problem in a SharePoint Web Part: Using $("'#" +ClientID + "'") doesn't work.

I found using $("'[id $=" + ClientID +"]'") worked (in IE8), where looking directly for the selector with the "#" didn't.


This looks like an ongoing issue with SharePoint 2010. I really wish someone would jump in and show us some guidance. I have a very similar situation and strugling for past 3 weeks. I like to redirect users to ThankYou or Finish page after they have taken a survey. Please post your findings or answers if you find one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.