0

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>
user2933504
  • 3
  • 1
  • 4
  • Most likely the change event is never triggered since the user isn't the source of that change. – Kevin B Nov 08 '13 at 19:07
  • I tested that ,actually the event is triggered if I change the textbox in code before the page gets loaded and it redirects to that page ,but if I put it in async which only completes after the page is loaded the event is not triggered . – user2933504 Nov 08 '13 at 19:09
  • Can someone suggest a better way to do this please – user2933504 Nov 08 '13 at 19:09
  • I personally wouldn't use any asp controls that write javascript for me, but that's just me. – Kevin B Nov 08 '13 at 19:10
  • Your edit looks right to me. Except you want `async: true` on the AJAX call. You need to figure out which part isn't being called, is it the AJAX method or the WebMethod? Try adding an `error` property on the AJAX options (just like you added a `success` property) which triggers an alert if there is an error calling the URL. You can output the details of the response, see [this question](http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages) for how to do that. – Trevor Elliott Nov 08 '13 at 20:20
  • I see the problem, change this `url: "Page.aspx/method"` to this `url: "Page.aspx/ConvertToBatch"` `method` is supposed to be the name of your method in the page. And `Page.aspx` is supposed to be the name of the page in which you defined the `ConvertToBatch` method. – Trevor Elliott Nov 08 '13 at 20:23
  • Thanks ,I added error property and the error "Unknown webmethod converttobatch" is thrown I had added [System.Web.Services.WebMethod()] already in my code what should I do now... – user2933504 Nov 08 '13 at 20:35
  • Sorry I added it wrong here since didn't want to say the pagename edited it now , but still the same error double checked I have the pagename and method name correct . – user2933504 Nov 08 '13 at 20:45
  • I resolved this error it seems the method should be declared as static if its inside an aspx page to be accessed in the ajax , the method gets called now but the loading message is not diaplayed ,can you please take a look – user2933504 Nov 08 '13 at 22:50

1 Answers1

1

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.

Community
  • 1
  • 1
Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • sorry I couldn't get you so will I be able to call this ajax function from server side passing the url ? – user2933504 Nov 08 '13 at 19:23
  • No, AJAX is called from the client side. – Trevor Elliott Nov 08 '13 at 19:26
  • sorry I haven't worked with ajax ,so when I browsed on a way to call c# method from ajax and get a value returned ,they say only when async=false will make it work ,but my method is async in c#.So will I be able to show loading message while I call a method from ajax and process it. – user2933504 Nov 08 '13 at 19:37
  • The point is that the user navigates to a URL. You show them a page which presumably has a "Loading..." message. Once the page loads it executes a method on the server which does whatever asynchronous work you need to do and finally returns a URL which was generated based on the work of the async method. Then the client page receives the returned URL and proceeds to navigate to it. – Trevor Elliott Nov 08 '13 at 19:56
  • I'm not sure what your exact requirements are because you never actually explained the problem you're trying to solve, you explained the solution that you came up with. It's possible that your solution was way off the mark and there is a better way of doing this. You'd have to edit your question to describe in plain English better detail as to what needs to happen and why. – Trevor Elliott Nov 08 '13 at 19:57
  • Tnanks for explaning ,I have added a ajax method in js file likw this – user2933504 Nov 08 '13 at 20:02
  • Keep in mind that most async methods *can* be called synchronously. I simply assumed that you don't want to do this because the user would then have to wait a potentially long time for the page to load. The point of AJAX is you don't want to take a very long time to load a page (more than a few seconds). You want pages to load as quick as possible while the bulk of the data loads in AJAX. – Trevor Elliott Nov 08 '13 at 20:02
  • $(document).ready(function () { $('#outputFromCmdLine').Text = "Loading..."; GetOutputFromCommandLine(); }); function GetOutputFromCommandLine() { $.ajax({ type: "POST", url: "somepage.aspx/methodname", //url to point your webmethod contentType: "application/json; charset=utf-8", dataType: "json", async:false, success: function (Result) { window.location.href = Result; }, error: function () { alert('error'); } }); – user2933504 Nov 08 '13 at 20:03
  • sorry I will add my edited code above ,here I cannot format it – user2933504 Nov 08 '13 at 20:04
  • Please see my edited code I have added a ajax call , as you had explained earlier my requirement is the user loads a page I show loading message and in the background I process an exe which will return a url at end and redirect the page to that url. – user2933504 Nov 08 '13 at 20:11
  • Was able to make it work ,Marking it as answer since I was able to implement following this ajax method – user2933504 Nov 11 '13 at 17:42