1

I need to call a javascript function(CommentButtonShow()) from c# code behind ajax method. I am unable to achieve this.

Following is the C# ajax method,

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

Below is the javascript function,

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

Please help me out.

Thanks.

6
  • I need to call a javascript function(CommentButtonShow()) from c# code behind ajax method. I am unable to achieve this. The CommentButtonShow method is not getting triggered Commented Apr 17, 2012 at 8:25
  • Did you look in your javascript debugging tool Console? Did you look whether the AJAX request is being sent (in your javascript debugging tool)? How does the request look like? What does the server respond to? Commented Apr 17, 2012 at 8:25
  • I am unsure about the javascript debugging tool Console. Correct me if I am wrong. I added an alert in the javascript function, but the functions is not getting triggered at all. Commented Apr 17, 2012 at 8:27
  • Oh no, alerting is not a javascript debugging tool. It was 15 years ago. Today FireBug is one example. Chrome developer tools is another example. And if you use IE I can't give an example as I don't use this browser to do development. Commented Apr 17, 2012 at 8:28
  • Thanks for that information. I will try it out. IS there any other 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. Your thoughts please. Commented Apr 17, 2012 at 8:32

3 Answers 3

1

You can call CommentButtonShow() function in the javascript inside the ajax call success event. you cannot call javascript function inside web methods. if it is a post back your code will work but for ajax call backs it wont work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks chamika. Is there any other way I can call JS method from code behind ajax method?
what is the purpose of calling javascript function inside web method if you are calling it using ajax
0

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. I mean, even you really cant invoke a javascript back into the server-side code in any programming language on earth!

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. 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. It does not mean that you have javascript events or such. 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 is why when the html of such controls is rendered you see a typical .net based ID generation which looks like _ctr01 and such.

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).

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.

Comments

0

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.

   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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.