1

I would like to call a code behind function from the client side. The function cannot be static - should modified unstatic variabels. I was able to create a non-visible button, and when I need that function I demonstrate a click on that btn. How can I send parameters to that code behind function?

InbalIta
  • 61
  • 8
  • 1
    can you share some code? javascript, controller, code behind – Michael Sander Aug 13 '15 at 07:26
  • here's your answer http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-side-from-client-side-using-javsscript – Vibhesh Kaul Aug 13 '15 at 07:35
  • 1
    It depends on what backend technology you would like to use : WebService (WCF), ASP.NET, ASP.NET MVC, Web API... For each of these you'll get different answers so please specify the one that you use although one thing is the same for all - the query string. You can make get requests like this : `http:// ... /mymethod?param1=value1&param2=value2` – Fabjan Aug 13 '15 at 08:00
  • @Inballta You are using web forms or asp.net mvc? – Nitin Sawant Aug 13 '15 at 09:08

3 Answers3

0
$.ajax({
    url: '/ControllerName/ActionName',
    type: "GET",
    data: { param1: val1 },
    success: function (res) {
        alert(res) // res is results that came from function
    }
});

This is the client side to call backend method. The server side to accept this request:

    public ActionResult ActionName(string param1)
    {
        return View(param1);
    }

In this case, we used jQuery, the javascript plugin, and we used AJAX request, also sending parameters.

Festim Cahani
  • 317
  • 4
  • 18
0

Using MVC and jQuery

Client Side ( Using Razor )

$.ajax({
    url: '@Url.Action("ActionName","ControllerName",new{parameters})',
    type: "GET",
    contentType: "application/json",
    data: { param1: val1 },
    success: function (res) {
        alert(res) // res is results that came from function
    },
    error: function (jqXHR, error, errorThrown) {
        console.log('An error as occured');
    },
});

Server Side

[HttpGet]
public JsonResult ActionName(string param1)
{
     return Json(param1, JsonRequestBehavior.AllowGet);
}

Note: HttpGet Verb is the default verb for every ActionResult/JsonResult

0

the button have a CommandArgument attribute that you can use to send a value to that function and you can read it as follow :

public void yourFunction(object sender,Eventargs e)
{
   string args = ((LinkButton)sender).CommandArgument.ToString();
   // rest of code here
}
Sora
  • 2,465
  • 18
  • 73
  • 146