AJAX:如何从AJAX方法背后的C#代码调用javascript函数 [英] AJAX: How to call javascript function from C# code behind AJAX method

查看:58
本文介绍了AJAX:如何从AJAX方法背后的C#代码调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要从ajax方法后面的c#代码中调用javascript函数( CommentButtonShow ()),但是JS方法根本没有被触发.有什么特定的原因可以解释为什么在c#ajax方法中没有触发Javascript方法?我在方法后面的普通代码中使用了相同的RegisterClientScriptBlock,并触发了JS函数.

以下是我的C#ajax方法,

Hi all,

I need to call a javascript function(CommentButtonShow()) from c# code behind ajax method but the JS method is not getting triggered at all. Is there any specific reason so as to why the Javascript method is not getting triggered in c# ajax method? I used the same RegisterClientScriptBlock in a normal code behind method and got the JS function triggered.

Following is my C# ajax method,

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



以下是javascript函数,



Below is the javascript function,

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



请发表您的想法.

预先感谢.



Your thoughts please.

Thanks in advance.

推荐答案

(" ).removeClass(' 隐藏') .addClass(' 显示'); }
("#ctl00_mainContentPlaceHolder_divEmailFriends").removeClass('hidden') .addClass('show'); }



请发表您的想法.

预先感谢.



Your thoughts please.

Thanks in advance.


您不会调用"来自C#的JavaScript方法.您可以尝试将JavaScript插入输出流.在这种情况下,您将通过AJAX执行带外方法,因此不会呈现任何输出流.如果要调用JavaScript方法,则需要在AJAX调用返回到客户端之后进行.

您应该对AJAX操作相对于传统的ASP.NET回发进行更多研究.
You don''t "call" a JavaScript method from C#. You can attempt to insert JavaScript into the output stream. In this case you are executing an out of band method, via AJAX, so there is no output stream being rendered. If you want to call the JavaScript method then you need to do so after the AJAX call has return to the client.

You should do more research on what is happening doing an AJAX operation vs. a traditional ASP.NET postback.


Page.ClientScript.RegisterStartupScript(this.GetType(), "alert1", "Hello();", true);


这篇关于AJAX:如何从AJAX方法背后的C#代码调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆