Pages

Thursday, March 21, 2013

Jquery Redirect After save an item from NewForm.aspx and EditForm.aspx

Hello all

I came across to an situation where user needs to redirect to a "Thank You" or lets say different page when they save item fron "NewForm.aspx" or "EditForm.aspx".

As I do not want to use Visual Studio code that ater save item Event Receiver - "ItemAdded" I tried to look around is there any other option available like Jquery , and yes we can , so easy :)

Here is the code for that.

$(document).ready(function() {

        var button = $("input[id$=SaveItem]"); 
// change redirection behavior 
button.removeAttr("onclick"); 
 button.click(function() { 
             var elementName = $(this).attr("name"); 
             var aspForm = $("form[name=aspnetForm]"); 
             var oldPostbackUrl = aspForm.get(0).action; 
             var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl); 
             var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "custompageurl.aspx"); 
   
             if (!PreSaveItem()) return false; 
             WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true)); 
         }); 
});

Enjoy Jquery!
Disha Shah

4 comments:

  1. Disha,

    Can I use this for the cancel button in 2010 ?

    Shannon

    ReplyDelete
  2. Uncaught TypeError: Cannot read property 'action' of undefined

    ReplyDelete
  3. Hi
    It redirects to new page.But it doesnt save the edited value in my case.
    Can you please help on this

    ReplyDelete