1

Is it even possible? To call a code-behind c# function from javascript in a visual web part?

It is a complex function so converting all my codes to client side is not an option. I want the logic that is there in this function to happen without a page refresh. This is the background of my issue.

Thanks guys..

Adil
  • 146,340
  • 25
  • 209
  • 204
user3057739
  • 31
  • 1
  • 5

2 Answers2

1

You can use jQuery ajax to call server side method and get the response to be used in javascript. This article has simple and good example to show what you need to do.

Code behind

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

Javascript

$.ajax({
  type: "POST",
  url: "PageName.aspx/MethodName",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Thanks. But the URL and the example you refered talks about page methods which cannot be used in user controls like viusal web part. correct? – user3057739 Apr 16 '14 at 12:40
  • Web parts are added to some aspx page isn't it? – Adil Apr 16 '14 at 12:42
  • Yes ofcourse. But I tried doing it but it did not work. The example you provided, I did try. Now i'm not sure if I did some mistake or this approach will not work. Let me try again. Thank you! – user3057739 Apr 16 '14 at 12:43
  • You need to add jQuery for it – Adil Apr 16 '14 at 12:44
  • Yes I did all those. I made sure I did not do any obvious mistakes. – user3057739 Apr 16 '14 at 12:46
  • When I put an alter in the function, alert(msg), the alter is throwing the whole HTML. Any clues? Thanks! – user3057739 Apr 16 '14 at 13:05
  • What you return from server side method? Use console.log(msg) to get the message structure, http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it – Adil Apr 16 '14 at 14:58
  • This is what I'm returning.. I will try out the console.log now. Thank you!! `[WebMethod] [ScriptMethod] public static String Server_HelloWorld() { return "Hello, How are you?"; }` – user3057739 Apr 18 '14 at 08:29
  • Try console.log(msg.d) and see what you get – Adil Apr 18 '14 at 08:52
  • The console is showing the entire ASPX page in the form of HTML. I saw the console in Firebug. Any other suggestions please.. – user3057739 Apr 18 '14 at 10:10
  • One difference in my code is, the class is a webpart and not a page, `public partial class SearchBox : WebPart` – user3057739 Apr 18 '14 at 10:11
  • Are you sure you get aspx page from msg.d? – Adil Apr 18 '14 at 10:11
  • Yes. SInce im doing all these in a web part and added the webpart to home.aspx im seeing the whole home.aspx in the console. my bad. `success: function (result){ alert("ajax --> " + result); }` – user3057739 Apr 18 '14 at 10:19
  • I think you need to make a test by using aspx and not web parts to check if it works – Adil Apr 18 '14 at 10:21
  • With aspx it will work. I already tried. So like I understood previously, this approach is not feasbile with user controles?? Guess I will not be certain about this anytime soon. Thank you for you time though. – user3057739 Apr 18 '14 at 10:23
0

Why don`t you use a Webservice (Ajax-Enabled WCF Service) which can be called via AJAX?

I think this would be the clean way. Put your logic in an extra class and use this class in the webservice and your webpart. Then you cann call the Method from Code and from Javascript.

Bjego
  • 665
  • 5
  • 14
  • Yes that is there. But I was wondering if we can do it without the help of a webservice. Using a webservice is not an option for me, unfortunatly, until I can prove that the asked approach dose not work. – user3057739 Apr 16 '14 at 13:06