开发者

Execute an ASP.net method from JavaScript method

I have a javascript method looks like this

JSMethod(JS_para_1,JS_para_2) { ...... ,,,,, }

a开发者_如何学JAVAnd I have an ASP.NET method like this

ASP_Net_Method(ASP_Para_1,ASP_Para_2) { .... ,,, }

Now I want to call this ASP_Net_Method from my JSMethod by passing some parameters over there..


Just to be clear:

  • Your javascript is executed by the user's browser on the user's laptop
  • Your ASP.NET method is executed on your server

So, what you probably want to do is to send a message from the browser to the server saying "Hey, run this method and give me the result back".

If you are doing traditional ASP.NET development (not ASP.NET MVC), I think the normal approach would be to create an ASPX page which, when requested, executes the method you want executed. Then, in your javascript you just need to request this page. To do this, you can use jQuery (either jQuery ajax, jQuery get or jQuery post).

You will need to download the jQuery library and include it in your page for this to work.

Give it a go and if you can't get it to work, come back for more specific advice.

EDIT: You can also take a look at ASP.NET AJAX. The home page has a lot of tutorials and videos.


What you really want to do is execute server-side code (sometimes called "Code-behind", which was the term I used when googling this.) from javascript.

This post shows several options. The better ones are toward the bottom.

http://forums.asp.net/t/1059213.aspx


Basically, every function that fires a server side event uses a javascript method called __doPostBack and here is an example of what you want to do.


Ajax's PageMethods is very useful for this if you don't want to do a full postback and just need to call 1 method.

First I decorate a method in my aspx.cs file like so:

  [System.Web.Services.WebMethod]
        public static string getVersions(string filePath)
        { ...
           return myString;
        }

Notice the "static" too. Then in javascript I can call this like:

PageMethods.getVersions(_hfFilePath.value, LoadVersionsCallback);

You can have as many parameters as you need of different data types. The last parameter is the JavaScript function that gets called when the call returns. Looks something like:

            function LoadVersionsCallback(result) {
    ...
     // I make a drop down list box out of the results:
     parts = result.split('|');
    for (var i = 0; i < parts.length; i++) {
          _ddl.options[_ddl.options.length] = 
                 new Option(parts[i].replace(/~/g, ",  "), parts[i]);
                }
 ...
}

Finally, you must have the script managers property "EnablePageMethods" set to "true".

           <ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" 
        runat="server" EnablePageMethods="true"
                EnablePartialRendering="true"
     OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
            </ajaxToolkit:ToolkitScriptManager>

So from JavaScript you can call a static function on your page's behind code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜