简体   繁体   English

使用asp.net中的codebehind函数执行客户端javascript函数

[英]execute clientside javascript function using codebehind function in asp.net

I have been using Anthem.NET in ASP.NET 2.0 projects for a few side projects and have a question regarding a slick piece of functionality that appears unique to this framework. 我在ASP.NET 2.0项目中一直在使用Anthem.NET进行一些辅助项目,并且对这个框架特有的光滑功能有疑问。 I have worked with Telerik and Microsoft ASP.NET AJAX Frameworks and the ability to execute a JavaScript function from within an "ajaxified" control in a code behind page (server side) seams to be missing. 我曾经使用Telerik和Microsoft ASP.NET AJAX框架,并且无法从页面(服务器端)接缝后面的代码中的“ ajaxified”控件中执行JavaScript函数。

The anthem example can be found here: http://anthem.talloaksoftware.com/InvokeClientSideFunctionAfterCallBack.aspx?1 国歌示例可以在这里找到: http : //anthem.talloaksoftware.com/InvokeClientSideFunctionAfterCallBack.aspx?1

Essentially, you wrap a textbox and a button with an Ajax Update Panel (in the Anthem Framework), and then in the codebehind (or inline code) page, you can execute a javascript function after your callback has completed. 本质上,您使用Ajax更新面板(在Anthem框架中)包装一个文本框和一个按钮,然后在代码隐藏(或内联代码)页面中,可以在回调完成后执行javascript函数。 But, your server side code actually specifies the javascript code that will run. 但是,您的服务器端代码实际上指定了将运行的javascript代码。

Example: The following code is executed when a button is clicked, which after performing the ajax request to the server, executes a JavaScript function that was specified server side. 示例:单击按钮时,将执行以下代码,该代码在向服务器执行ajax请求后,将执行服务器端指定的JavaScript函数。

Anthem.Manager.AddScriptForClientSideEval("ClientSideFunction();");

So, as long as the JavaScript function "ClientSideFunction()" exists, it will execute after the callback completes. 因此,只要存在JavaScript函数“ ClientSideFunction()”,它将在回调完成后执行。 I can immediately see how the inclusion of jQuery and utilizing this feature would make a very interesting combination. 我可以立即看到包含jQuery并利用此功能如何进行非常有趣的组合。 Code could still be written in the traditional postback model, but the JavaScript code could be called after the server side code is executed. 仍然可以使用传统的回发模型编写代码,但是可以在执行服务器端代码之后调用JavaScript代码。 This would allow server side code to "animate" the HTML objects using jQuerys built in functions. 这将允许服务器端代码使用内置函数的jQuery对HTML对象进行“动画处理”。

My question is, how to duplicate this behavior using ASP.NET AJAX 3.5 built into .NET Framework 3.5? 我的问题是,如何使用内置在.NET Framework 3.5中的ASP.NET AJAX 3.5复制此行为? Can it be duplicated as easily as the Anthem.NET framework provides? 是否可以像Anthem.NET框架一样容易地复制它?

Anthem is not an active project anymore (on sourcefourge), so I feel it is wise to build new projects using the supported and popular ASP.NET AJAX Framework (especially with 4.0 coming), so I am looking for an equivalent solution. Anthem不再是一个活跃的项目(在sourcefourge上),因此,我认为使用受支持的流行的ASP.NET AJAX框架(尤其是4.0版)来构建新项目是明智的,因此,我正在寻找一个等效的解决方案。

Really simple, you'd to have a ScriptManager on the page then server-side, when the updatepanel is updating (before the method .Update() ) just use: 真的很简单,在updatepanel更新时(在方法.Update()之前),您需要在页面上然后在服务器端使用ScriptManager:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "uniqueJSBlockName", "ClientSideFunction();", true);

will execute the function called ClientSideFunction(), you can hold the necessary js functions already registered on the page. 将执行名为ClientSideFunction()的函数,您可以保留已在页面上注册的必要js函数。

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

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