简体   繁体   English

如何在函数形式的Java脚本函数后面调用代码?

[英]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. 从这个function1()这是一个javascript函数,我想调用一个用c#代码编写的函数,以便我可以访问页面的控件并执行我想做的任何操作。

You cannot call C# directly from javascript on your website. 您不能直接从网站上的javascript调用C#。 You need to use AJAX for this. 您需要为此使用AJAX。 http://en.wikipedia.org/wiki/Ajax_%28programming%29 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. 如果您使用的是mvc,则将您的方法作为对控制器的操作公开,并使用ajax进行调用。

Or use pagemethods 或使用pagemethod

Expose yourmethod as a 'webmethod', eneable pagemthods in your Scriptmanager 在脚本管理器中将您的方法公开为“网络方法”,可使用的页面方法

and you can call by in your js by: 您可以通过以下方式在您的js中调用:

PageMethods.YourMethodName(yourParam,aCallBackFunction)

Then create a callback function in js to do something with your result. 然后在js中创建一个回调函数,以对您的结果进行处理。

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: 亲爱的所有人,我在javascript函数中找到了这个简单答案,您需要使用以下参数名称将其回发到ur页面:

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

then in .cs page u need to catch this in page_load event like this : 然后在.cs页面中,您需要在page_load事件中捕获此事件,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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