简体   繁体   English

如何通过Javascript或Jquery调用代码隐藏的方法

[英]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 我的Javascript代码中有以下代码行

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. 但是,当我将调试器放入javascript代码中时,credenciadausuario变量会收到字符串“ <%= getCredenciada()%>”,而不是返回我的方法。 How can I call my method that are in my code-behind via javascript or jquery ? 如何通过javascript或jquery调用代码隐藏的方法?

It seems all you want to do in your code is get the value of a cookie. 您似乎想要在代码中做的就是获取cookie的值。 Why not do that in JavaScript on the client? 为什么不在客户端的JavaScript中这样做呢?

IF possible make use of ajax and do call the method, that will do you task. 如果可能,请使用ajax并调用该方法,这将为您完成任务。

check this post : http://pranayamr.blogspot.com/2012/01/calling-server-side-function-from.html 检查此帖子: http : //pranayamr.blogspot.com/2012/01/calling-server-side-function-from.html

Cs File (codebehind) Cs文件(代码隐藏)

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

Javascript Java脚本

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

为了进一步补充Pranay的答案,此链接提供了一个很好的教程。

This article from Encosia is excellent. 恩科西亚的这篇文章很棒。 It shows how to call a method in your code behind using jQuery ajax. 它显示了如何在使用jQuery ajax的背后调用代码中的方法。

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

In your code behind you have to give the method the [WebMethod] attribute: 在后面的代码中,必须为该方法赋予[WebMethod]属性:

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: 要使用jQuery调用该方法,您可以使用以下代码:

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

Well, to actually CALL your code-behind methods from Javascript, you would have to use ajax. 好吧,要从Javascript实际调用您的代码隐藏方法,您将必须使用ajax。 JQuery has a nice $.ajax wrapper for that. jQuery为此提供了一个不错的$ .ajax包装器。

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. 但是我认为您只想在将js代码生成并发送到浏览器时将其包含一次。 In that case, you need to use a file type which ASP.NET recognizes as a dynamic file. 在这种情况下,您需要使用ASP.NET识别为动态文件的文件类型。

The easiest would be to put JS code (in a <script> tag) into .ascx files. 最简单的方法是将JS代码(在<script>标记中)放入.ascx文件中。 Then <%= getCredenciada() %> will be executed and will return an actual string which will be rendered into javascript code. 然后将执行<%= getCredenciada() %> ,并将返回一个实际的字符串,该字符串将呈现为javascript代码。

Then, of course, you should include such a control to the page as a regular ASP.NET control. 然后,当然,您应该将此类控件作为常规ASP.NET控件添加到页面中。

And I am not saying this is the best way to achieve what you want. 我并不是说这是实现所需目标的最佳方法。 Sometimes it's just the fastest. 有时只是最快。

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

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