Question

in my asp.net application, two buttons call a javascript function called refreshView, below is the function refreshView.

function RefreshView() {
                        __doPostBack('ButtonApply', '')
                        window.parent.location.href = "dashboardtree.aspx"
                    }

In IE, this is working correctly, but in firefox and safari the page refreshes (due to the window.parent.location) - but never calls the doPostBack (i was able to tell this using tracepoint and the sub that is called stores values to the database, the values are not being stored as well, sub is never hit). Below is the first line of the sub.

 Private Sub ButtonApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonApply.Click

Can anyone see why this would not work? I figured I could always replace the doPostBack with pageMethods and create a new sub/function - but I'd like to complete this the way it currently is. Thank you.

Was it helpful?

Solution

Don't perform a redirect and postback at the same time in your JavaScript routine. What you should do instead is to redirect after your postback logic has completed. You will only know when it is completed in the Codebehind or if your Javascript received some sort of Asynchronous callback. In its current state, your JavaScript routine has no idea when your Postback will complete.

You are creating a conflict. What you are essentially doing is telling the page to post and at the very same time to redirect somewhere else. In all likelihood, FireFox and Safari probably ignored the __doPostBack() because of this fact.

Here's what I would do instead:

public void Page_Load(object sender, EventArgs e)
{
  string target = Request["__EVENTTARGET"];
  //if parameter equals "ButtonApply"
  //{       
  //    Do whatever, Then...
  //    Response.Redirect("dashboardtree.aspx"); <- you may need to modify the url depending on your structure
  //}
}

EDIT

...about the response.redirect - this will not work for me because the page that contains the post back, is an iframe inside another page...

Apologies, I missed the window.parent.

What I would recommend then is to use ajax. In a nutshell, you would use ajax to place a call to a server-side method which would perform your logic (that you currently do in the postback). Upon completion, the client will be notified in a callback when the process has completed (and whether or not it was successful). Upon successful completion, you would perform your redirect.

There are several examples of this on the web, I'll provide a few for you:

OTHER TIPS

In order to execute a method after each postback is finished, you can add the method to the handlerlist via PageRequestManager. (see : http://msdn.microsoft.com/en-us/library/bb311028) After the method is executed you can remove it from the handler list if necessary.

function Reload() {
    Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(Reload);
    window.parent.location.href = "dashboardtree.aspx";
}

function RefreshView() {
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(Reload);
    __doPostBack('ButtonApply', '');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top