简体   繁体   中英

how to call a code behind function form java script function?

I have a div like this :

<div id="div1" style="width: 100%; height: 30px; background-color: Blue" onclick="function1()"></div> 

from this function1() which is a javascript function I want to call a function which written in c# code behind so that i can access the controls of the page and do my manipulation whatever i want to do.

You cannot call C# directly from javascript on your website. You need to use AJAX for this. http://en.wikipedia.org/wiki/Ajax_%28programming%29

If you are using mvc expose your method as an action on a controller and call with ajax.

Or use pagemethods

Expose yourmethod as a 'webmethod', eneable pagemthods in your Scriptmanager

and you can call by in your js by:

PageMethods.YourMethodName(yourParam,aCallBackFunction)

Then create a callback function in js to do something with your result.

Dear all, I found this simple answer in ur javascript function u need to do a postback to ur page with the argument name like this:

function function1() {
   __doPostBack('', 'MyButtonClick');
}

then in .cs page u need to catch this in page_load event like this :

if (Page.Request.Params["__EVENTARGUMENT"] == "MyButtonClick")
{
   function1();//this will take u to ur .cs function 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM