user2933504
user2933504

Reputation: 13

Redirect a page inside a async method

Requirement: User calls a page and I display page is loading while i execute a method in background and redirect to the url that the method returns.

Tried using async method call in pageload ,pagenotredirected as already loaded. Tried updating textbox from the async method and call textboxonchange not working as page aleady loaded Have added the code for ajax per ur suggestion please review

I call a async method inside pageload and in the pageload I display loading message ,after the async methos completes I get a url ,iam trying to redirect to that url but iam unable to do it inside the async as the page is already loaded ,so iam updating an hidden textbox with this url and using the onchange event in that text box and redirecting but still the page is not redirected,can someone please suggest a better way to do this-Thanks

 <asp:TextBox runat="server" ID="urlTextBox" Value="" Style="display:none;" AutoPostBack="true" ></asp:TextBox>

  $(document).ready(function () {
  $('#urlTextBox').change(function () {
             var keyword = $("#urlTextBox").val();
             if (keyword.length > 0) {
                 window.location.href = keyword;
             }       
         });

   urlTextBox.Text = url;

the urltextbox gets value inside the async method as I process a long running process inside the async to get this url

I have added a js file in which I call the c# method like this

 $(document).ready(function () {
  $('#outputFromCmdLine').Text = "Loading...";
  GetOutputFromCommandLine();   
 });

 function GetOutputFromCommandLine() {  
  $.ajax({
    type: "POST",
    url: "Page.aspx/ConvertToBatch", //url to point your webmethod     
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async:true,
    success: function (Result) {
        window.location.href = Result;
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});

and the c# method is sync method and returns the url like this

  [System.Web.Services.WebMethod()]
    public static string ConvertToBatch()
    {
   some process
   return urlstring;
    }

I also added the js file name in top of aspx page ,but nothing gets called ,is this the right way to do it

 <script type="text/javascript" src="/Scripts/jqueryFile.js"></script>

Upvotes: 1

Views: 3141

Answers (1)

Trevor Elliott
Trevor Elliott

Reputation: 11252

Instead of calling the async method from inside the page load you should call it using AJAX in JavaScript once the page is loaded. Then you can use the response from the AJAX request to retrieve the URL with which to trigger navigation to.

<script>
    $(function()
    {
        $.ajax(
        {
            url: "/api/request",
        }).done(function(responseUrl)
        {
            window.location.href = responseUrl;
        });
    });
</script>

This will wait for the page to be loaded, make some request to the server, and navigate to the response string (expecting the HTTP response body to be a URL).

I think you would use an ASMX web service or better yet an ASP.NET Web API controller to handle the actual request/response.

I know that with Visual Studio 2013 you can create an ASP.NET Web Forms project which includes Web API and configures them automatically to work together. If you are working with an existing project you could add Web API as a NuGet package and add the appropriate plumbing to make it work manually by simply looking at the VS2013 wizard-generated project as an example of how it is done.

EDIT:

It seems you found another way to handle AJAX calls which is using a WebMethod. WebMethods are described in this question.

Upvotes: 1

Related Questions