1

I need to call a JavaScript function that shows a Bootstrap modal. I have the following code:

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static void execModal(string groupname, int zw) { 
    //need to generate the data first
    StringBuilder sb = new StringBuilder();
    Generate gen = new Generate();
    sb = gen.generateDeeperTable(zw, groupname);

    //insert into my placeholder
    //Append the HTML string to Placeholder.
    fillPH(sb);

    //run javascript function <showModel> to show the modal

}

Here is how I call the execModal method form JS:

    <script type="text/javascript>
       function callGenDeeperTable(zw, groupname) {

          PageMethods.execModal(groupname, zw);
       }
   </script>

The function execModal is called from a javascript function in the .aspx site.

How can I call the javascript function showModal?

Hack4Life
  • 523
  • 11
  • 33
  • call modal window in js after (or before) calling web-service – Backs Aug 25 '15 at 07:28
  • How can I do this @Backs ? – Hack4Life Aug 25 '15 at 07:29
  • how do you call web service? and what modal window do you want to show: bootstrab, jquery or simple js-confirm? your question is too broad – Backs Aug 25 '15 at 07:31
  • I have added the JS function. I want to show a bootstrap modal as written in the first sentence. – Hack4Life Aug 25 '15 at 07:36
  • What client side AJAX framework are you using to call this? Also, what is fillPH(). I'm assuming you're trying to generate some HTML string and stick it in a place holder control - that will not work. In a (static) WebMethod you cannot do that, unless fillPH is a static method that is using the current execution context to get the page. You could either have your WebMethod return something other than void (ie. the name of the js function you want to fire, or a bit of script to inject on completion). On your client you want something [like this](http://api.jquery.com/jquery.ajax/) – Eric Lease Aug 25 '15 at 07:37
  • http://getbootstrap.com/javascript/#modals – Backs Aug 25 '15 at 07:37
  • I think that I do not use any AJAX framework. I have never worked with AJAX... Yes @EricLease `fillPH` should fill a Placholder. Current Execution context? Do you have an example on that? – Hack4Life Aug 25 '15 at 08:12

2 Answers2

4

enter image description here

Your execModal method is on the server. You have not specified where you want to invoke (call) it from, but since you've decorated it (added attributes to the method defining it...) as a WebMethod, chances are you're trying to invoke it from a (HTML) page running in a client's browser. To make this call you need a line of communication between the client that wants to run the method, and the server that holds the method. You will also notice that execModal is defined as a static method. That means that when it is invoked it will not have the instance members of the Page class, including things like fillPH (unless fillPH is static). I don't know if you're working with ASP.NET WebForms (trying to make the call from an .aspx page), or this is a service consumed by some application (method resides in an .asmx), or I guess this could even be ASP.NET MVC.


Assuming ASP.NET WebForms

Let's deal with the simplest case since there have been almost no details provided. If we assume that this method, execModal, lives in an .aspx.cs file, and you're trying to call it from the corresponding .aspx page, and this is part of an ASP.NET WebForms application...

  1. You need to initiate the call to execModal. This requires an AJAX call from your client to your server. You can create your own AJAX framework, but there are many open source frameworks available. I will give an example below using jQuery.

  2. You need to do the work on the server statically, or you need to use the HttpCurrent.Context to get the instance of the Page, Session, etc. The Page can be retrieved via the HttpCurrent.Context.Handler as Page.

  3. Once the method finishes on the server, the result (success or failure), and optionally any data you want to return to the client, is sent back to the client's browser. The client portion of the application should be able to process this response with an event handler. The event handler should be associated with the XMLHttpRequest object's onreadystatechange event and process the response when the state changes to 4 (done). Frameworks, like jQuery, take care of this overhead by providing parameters in your AJAX call to specify the success/failure callbacks. Do not confuse the result (sucess/failure) of this communication process with the result of your application's processes (actual work of execModal).

  4. Your client side (success) callback function would then just call your desired js function showModal.

Your client side AJAX call (if you were to use jQuery) would look something like this...

$.ajax({
    type: "POST",
    url: "Default.aspx/execModal",
    data: '{groupname: "' + groupname + '", zw: ' + zw + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        showModal();
    },
    failure: function(response) {
        alert("AJAX request failed");
    }
});

This is a very basic example. You might want the server to decide what happens on success, but returning a JSON data, or a string with the name of a js function that can be executed on the client - these are just examples to get your thinking about what is possible.


Edit - account for OP's addition of PageMethods.execModal() to question

You say you're calling your server code from this...

<script type="text/javascript>
   function callGenDeeperTable(zw, groupname) {

      PageMethods.execModal(groupname, zw);
   }

That implies that you're using ASP.NET's ScriptManager with EnablePageMethods=true. Is that correct?

That would inject the script with the PageMethods type, and define functions for each of your WebMethods. So you are using an AJAX framework - the one provided by MS. All that work of making the AJAX call is being hidden from you with PageMethods.execModal(groupname, zw). However, each of the functions generated for you (on the PageMethod object) take additional parameters for OnSuccess and OnFailure callbacks. Here is a SO answer detailing how to use the ScriptManager's PageMethod object.

The signature for any function generated on the PageMethod object corresponding to a WebMethod is...

function foo(param1, param2, param3, etc., OnSuccess, OnFailure)

In your case, you have two parameters being passed to the WebMethod, so after those two parameters you would want to supply callback functions for success and failure. showModal would be your success handler probably...

PageMethods.execModal(groupname, zw, showModal, function() { alert('failed!') });
Community
  • 1
  • 1
Eric Lease
  • 3,894
  • 1
  • 24
  • 43
1

this is what i did and worked like charm

javascript part

   function buyproduct(productId) 
   {
        PageMethods.checkifvalid(productid, OnSuccess);
   }                             

   function OnSuccess(response, userContext, methodName) 
   {
        this[response]();
   }

   function functionToCallAfterwebmethod()
   {
        alert("Javascript Function called successfully!!");
   }

Aspx.cs part

    [System.Web.Services.WebMethod]
    public static string checkifvalid(int productId)
    {

        --your any logic to check

        -- pass the javascript function as return parameter

        return "functionToCallAfterwebmethod"
    }
Atul Takekar
  • 43
  • 12
  • Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, *tailor your answers to the question.* – Paul Roub Jun 22 '16 at 19:32
  • I am sry to ask this , but new here. How can i flag a question as duplicate? – Atul Takekar Jun 22 '16 at 19:39
  • You'll have to gain a little bit more reputation before you can flag duplicates, I'm afraid. – Martijn Pieters Jun 23 '16 at 07:54