Is it possible for a Javascript function to call a method that is in the Behind code of the page?

Asked

Viewed 11,622 times

3

I have a button and I wanted to program the click of it, and when I wear a <asp:Button> I program the click of him in the code Behind. Now I’m wearing a button normal.

I know almost nothing about Javascript and wanted to program it on code Behind also. I could create a Javascript function by calling a C method#?

4 answers

3

Yes. You’ll need the Ajaxextensionstoolbox in your project.

First, put on your page the following:

<asp:ScriptManager ID="ScriptManager1" 
    EnablePageMethods="true" 
    EnablePartialRendering="true" runat="server" />

In the Code Behind, create the static method:

using System.Web.Services;

[WebMethod]
public static string MeuMetodo(string nome)
{
    return "Oi, " + nome;
}

To call, you can do so:

<script type="text/javascript">
    $("#botao").click(function () {
        PageMethods.MeuMetodo("Cigano Morrison Mendez");
    });
</script>

For example, I suppose you have jQuery in your project.

3

If your implementation is restricted to an ASP.NET codebehind page, you can implement a call to a Webmethod as follows:

Mark the method as a Webmethod. It must necessarily be a static method.

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

Call the function directly from the javascript implementation.

$.ajax({
  type: "GET",
  url: "Default.aspx/MetodoASerChamado",
  success: function(msg) {
    // Do something interesting here.
  }
});

2

Directly is impossible but ASP.Net has facilities for the customer call invoking what they need:

<asp:Button ID="botao" OnClick="botao_Click" runat="server" />

Then in the Code-Behind:

protected void botao_Click(object sender, EventArgs e) {
   //faça o que quer aqui
}

I put in the Github for future reference.

  • I’m using a bootstrap button so it comes as a button, and if I switch to Asp:button, it won’t look the same.

  • You should have informed this in the question. I answered what you asked.

  • @Lucassousa Voce can use as the bigown put only use the bootstrap css <Asp:Button ID="boot" Onclick="boot_Click" Cssclass="btn btn-small btn-Primary" runat="server" />

1


first step, in the server-side add the method.

[WebMethod]
public static string Message(){
     return " MENSAGEM ";
}

Step 2, look for the tag <asp:ScriptManager ID="..." runat="server" /> and add EnablePageMethods="true"

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Step 3, let’s create a method in javascript

function getMessage() {
    PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}

Step four, let’s use a normal boot...

<input type='submit' value='Get Message' onclick='getMessage();return false;' />
  • When I click the button it appears the error : Uncaught Referenceerror: Pagemethods is not defined.

  • look for the tag asp:ScriptManager in his .aspx and add property EnablePageMethods="true"

  • It worked, but I created a method that returns an integer (1+1) and put it in a var in javascript and Alert to display this variable, but it shows undefined, Do you know what might be ? var result = PageMethods.PesquisarClick();

  • If it worked you can mark it as a correct answer or give a + For ease I found this tutorial , follow the instructions you will get

Browser other questions tagged

You are not signed in. Login or sign up in order to post.