Lucas_Santos
Lucas_Santos

Reputation: 4740

How can I call a method that are in my code-behind via Javascript or Jquery

I have the follow line in my Javascript code

credenciadausuario = '<%= getCredenciada() %>';

In my code-behind I have this method

public string getCredenciada()
{
    Utilidade.QuebraToken tk = new Utilidade.QuebraToken();
    string credenciada = tk.CarregaToken(1, Request.Cookies["token"].Value);
    return credenciada;
}

but when I put the debugger in my javascript code, the credenciadausuario variable, receives the string "<%= getCredenciada() %>" and not the return of my method. How can I call my method that are in my code-behind via javascript or jquery ?

Upvotes: 1

Views: 459

Answers (4)

Erikk Ross
Erikk Ross

Reputation: 2183

This article from Encosia is excellent. It shows how to call a method in your code behind using jQuery ajax.

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

In your code behind you have to give the method the [WebMethod] attribute:

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

To call that method using jQuery you would use the following:

$.ajax({
  type: "POST",
  url: "PageName.aspx/GetDate",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
   // Do something interesting here.
 }
});

Upvotes: 1

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34591

Well, to actually CALL your code-behind methods from Javascript, you would have to use ajax. JQuery has a nice $.ajax wrapper for that.

But I think you just want to include some value into the js code once, while it's being generated and sent to the browser. In that case, you need to use a file type which ASP.NET recognizes as a dynamic file.

The easiest would be to put JS code (in a <script> tag) into .ascx files. Then <%= getCredenciada() %> will be executed and will return an actual string which will be rendered into javascript code.

Then, of course, you should include such a control to the page as a regular ASP.NET control.

And I am not saying this is the best way to achieve what you want. Sometimes it's just the fastest.

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

IF possible make use of ajax and do call the method, that will do you task.

check this post : http://pranayamr.blogspot.com/2012/01/calling-server-side-function-from.html

Cs File (codebehind)

[WebMethod] 
public static string IsExists(string value) 
{     
    //code to check uniqe value call to database to check this     
   return "True";
 } 

Javascript

function IsExists(pagePath, dataString)
 {
  $.ajax({
     type:"POST",
     url: pagePath,
     data: dataString,
     contentType:"application/json; charset=utf-8",
     dataType:"json",
     error:
          function(XMLHttpRequest, textStatus, errorThrown) {               
               alert("Error");
          },
     success:
          function(result) {
                  alert( result.d);

      }
     }
});}

      var pagePath = window.location.pathname + "/IsExists";
     var dataString = "{ 'value':'ab" }";
     IsExists(pagePath, dataString);

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

It seems all you want to do in your code is get the value of a cookie. Why not do that in JavaScript on the client?

Upvotes: 2

Related Questions