Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
$("#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
$('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){
    $(this).click(function(){
            STSNavigate(goToWhenCanceled);
      })
});

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

/*
// Use this for adding a "static" redirect when the user submits the form
$(document).ready(function(){
    var goToWhenSubmitted = '/test/English/ThankYou.aspx';
    setOnSubmitRedir(goToWhenSubmitted);
});
*/

// 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
    setOnSubmitRedir(dynamicRedirect);
    alert(dynamicRedirect);
    // This function must return true for the save item to happen
    return true;
}

function init_fields(){
  var res = {};
  $("td.ms-formbody").each(function(){
      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;
}
</script>
share|improve this question
    
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 Sep 6 '11 at 22:12
    
It's an internal site. –  Tony Stark Sep 7 '11 at 15:51
    
Why not use an asp form action instead? –  TeckniX Sep 12 '11 at 13:10
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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