简体   繁体   English

AJAX:如何从AJAX方法背后的C#代码调用javascript函数

[英]AJAX: How to call javascript function from C# code behind AJAX method

I need to call a javascript function( CommentButtonShow() ) from c# code behind ajax method. 我需要从ajax方法背后的c#代码调用javascript函数( CommentButtonShow() )。 I am unable to achieve this. 我无法实现这一目标。

Following is the C# ajax method, 以下是C#ajax方法,

[AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
public string CheckPassword()
{
  ScriptManager.RegisterClientScriptBlock(this, GetType(), "ScriptManager1", "javascript:CommentButtonShow();", true);
}

Below is the javascript function, 以下是javascript函数,

    function CommentButtonShow() {
        $("#ctl00_mainContentPlaceHolder_divEmailFriends").removeClass('hidden').addClass('show'); 
    }

Please help me out. 请帮帮我。

Thanks. 谢谢。

You can call CommentButtonShow() function in the javascript inside the ajax call success event. 您可以在ajax调用成功事件内的javascript中调用CommentButtonShow()函数。 you cannot call javascript function inside web methods. 您不能在网络方法内调用javascript函数。 if it is a post back your code will work but for ajax call backs it wont work. 如果是回发,则您的代码将起作用,但对于ajax回叫,它将不起作用。

I am answering this to shed more light for those who are new to this cilent-side and server-side scripting and mix up when doing some interesting work to them :) 我正在回答这个问题,以便为那些对这种精通客户端和服务器端脚本的人更加了解,并在对他们进行一些有趣的工作时将它们混在一起:)

This is absolutely impossible to call a Javascript (client-side) function from within C# (server-side) code. 从C#(服务器端)代码中调用Javascript(客户端)函数绝对是不可能的。 I mean, even you really cant invoke a javascript back into the server-side code in any programming language on earth! 我的意思是,即使您真的无法以任何一种编程语言将javascript重新调用回服务器端代码中!

It is very simple. 这很简单。 Server-Side code renders the client-side code. 服务器端代码呈现客户端代码。 Since the client code (html/javascript) resides in the browser you don't have any hook for javascripts at least to get it in server-side and then invoke. 由于客户端代码(html / javascript)驻留在浏览器中,因此至少没有在服务器端获取javascript并进行调用的钩子。 But for Asp.Net provides you a hook that actually transforms client-side HTML controls (most HTML tags) into server-side controls (as the .net framework supports) and then you can access their properties and some methods which only invoke at server-side. 但是,对于Asp.Net,您可以通过钩子将客户端HTML控件(大多数HTML标记)实际转换为服务器端控件(.net框架支持),然后可以访问它们的属性和某些仅在服务器上调用的方法-侧。 It does not mean that you have javascript events or such. 这并不意味着您有javascript事件或类似事件。 Whatever you do with those server-side controls HAPPEN only at server-side and everything with that is COMPLETED before the final code of THAT control is sent to the browser to render. 使用这些服务器端控件进行的任何操作都只会在服务器端发生,并且在将THAT控件的最终代码发送到浏览器进行渲染之前,所有这些操作都已完成。 That is why when the html of such controls is rendered you see a typical .net based ID generation which looks like _ctr01 and such. 这就是为什么当呈现此类控件的html时,您会看到典型的基于.net的ID生成的样子,类似于_ctr01等。

Anyway, using Ajax (at client-side) you can still invoke server-side methods using Ajax.Net and/or Ajaxpro (ajaxpro.info) or a custom javascript lib (jquery). 无论如何,使用Ajax(在客户端),您仍然可以使用Ajax.Net和/或Ajaxpro(ajaxpro.info)或自定义javascript lib(jquery)来调用服务器端方法。

I hope this helps only in understanding what you are doing is actually NOT possible. 我希望这仅有助于了解您实际上在做什么。 I still would not rate your question negative as it is really going to help many new comers to understand how things work and how people who have gone through this got it right. 我仍然不会否定您的问题,因为它确实可以帮助许多新手了解事情的运作方式以及经历过此事的人们如何正确解决问题。

I hope its very fair use of this forum to provide the information that helps everybody rather negating their points without letting them know what they are asking/answering is right/wrong. 我希望它非常公平地利用这个论坛来提供信息,以帮助每个人宁可否定自己的观点而又不让他们知道自己在问/答的是对/错。

Thanks a lot. 非常感谢。

We can call a method in codebehind from the JQuery AJAX call and depending upon the status whether it is error or success the corresponding method will be executed. 我们可以在JQuery AJAX调用的代码背后调用一个方法,并根据状态是错误还是成功来执行相应的方法。

   function MyMethod() {
    $.ajax({
    type: "POST",
    url: "CodeBehind.aspx/ClearData",
    contentType: "application/json;charset=utf-8",
    data: '',
    dataType: "json",
    success: function (data, textStatus) {
        closePopUpwindow1();
    },
    error: function (data, textStatus) {
        closePopUpwindow2();
    }
});}

    [WebMethod]
   public static void ClearData(){
 Page.SetGridSessionData(gridID, null);
}

If the server side method is successfully executed then closePopUpwindow1 method is executed else closePopUpwindow2 method will be executed. 如果服务器端方法成功执行,则执行closePopUpwindow1方法,否则将执行closePopUpwindow2方法。

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

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