c# - Calling JavaScript function from [WebMethod] -


i need call javascript function shows bootstrap modal. have following code:

[system.web.services.webmethod()] [system.web.script.services.scriptmethod()] public static void execmodal(string groupname, int zw) {      //need generate data first     stringbuilder sb = new stringbuilder();     generate gen = new generate();     sb = gen.generatedeepertable(zw, groupname);      //insert placeholder     //append html string placeholder.     fillph(sb);      //run javascript function <showmodel> show modal  } 

here how call execmodal method form js:

    <script type="text/javascript>        function callgendeepertable(zw, groupname) {            pagemethods.execmodal(groupname, zw);        }    </script> 

the function execmodal called javascript function in .aspx site.

how can call javascript function showmodal?

enter image description here

your execmodal method on server. have not specified want invoke (call) from, since you've decorated (added attributes method defining it...) webmethod, chances you're trying invoke (html) page running in client's browser. make call need line of communication between client wants run method, , server holds method. notice execmodal defined static method. means when invoked not have instance members of page class, including things fillph (unless fillph static). don't know if you're working asp.net webforms (trying make call .aspx page), or service consumed application (method resides in .asmx), or guess asp.net mvc.


assuming asp.net webforms

let's deal simplest case since there have been no details provided. if assume method, execmodal, lives in .aspx.cs file, , you're trying call corresponding .aspx page, , part of asp.net webforms application...

  1. you need initiate call execmodal. requires ajax call client server. can create own ajax framework, there many open source frameworks available. give example below using jquery.

  2. you need work on server statically, or need use httpcurrent.context instance of page, session, etc. page can retrieved via httpcurrent.context.handler page.

  3. once method finishes on server, result (success or failure), , optionally data want return client, sent client's browser. client portion of application should able process response event handler. event handler should associated xmlhttprequest object's onreadystatechange event , process response when state changes 4 (done). frameworks, jquery, take care of overhead providing parameters in ajax call specify success/failure callbacks. not confuse result (sucess/failure) of communication process result of application's processes (actual work of execmodal).

  4. your client side (success) callback function call desired js function showmodal.

your client side ajax call (if use jquery) 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 basic example. might want server decide happens on success, returning json data, or string name of js function can executed on client - these examples thinking possible.


edit - account op's addition of pagemethods.execmodal() question

you you're calling server code this...

<script type="text/javascript>    function callgendeepertable(zw, groupname) {        pagemethods.execmodal(groupname, zw);    } 

that implies you're using asp.net's scriptmanager enablepagemethods=true. correct?

that inject script pagemethods type, , define functions each of webmethods. you using ajax framework - 1 provided ms. work of making ajax call being hidden pagemethods.execmodal(groupname, zw). however, each of functions generated (on pagemethod object) take additional parameters onsuccess , onfailure callbacks. here so answer detailing how use scriptmanager's pagemethod object.

the signature function generated on pagemethod object corresponding webmethod is...

function foo(param1, param2, param3, etc., onsuccess, onfailure) 

in case, have 2 parameters being passed webmethod, after 2 parameters want supply callback functions success , failure. showmodal success handler probably...

pagemethods.execmodal(groupname, zw, showmodal, function() { alert('failed!') }); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -