Q8Y
Q8Y

Reputation: 4001

how to send values from javascript to server side(asp.net)?

What is the best way to send values from JavaScript (client-side) to the server (asp.net) without refreshing the page?

I want to insert the data into a database but I don't want to refresh the page or change any data on it.

Upvotes: 1

Views: 10452

Answers (5)

maplemale
maplemale

Reputation: 2046

No one actually answered this question. As, "Import jquery" "use ajax" etc. all negate the fact that the OP asked for a javascript solution. This is a one-liner in javascript / very easy to do.

In your javascript you just call the method:

PageMethods.SomeMethod('test');

Your "SomeMethod" would be a code behind method like this:

[WebMethod]
    public static string SomeMethod(string param1)
    {
        string result = "The test worked!";
        return result;
    }

Rules: You have to identify your code behind method with a WebMethod attribute. It has to be static. And you have to register a script manager in your page as follows:

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

Since I am working with an aspx webforms page to do some really simple javascript functions like retrieving / stashing geo location, I put it inside the Form element as required.

Upvotes: 1

Marwan
Marwan

Reputation: 2402

Simple way :

1- Import jquery into your page
2- create ur function in the cs file of the page like this

     [WebMethod]
    public static string Save(string param1,string param2)
    {
        string result;
        //your code must return somthing string
        return result;
    }

3- in your aspx page create your function

function save(){
var param1 = 'value 1 you need to send to the server should be string';
var param2 = 'value 2 you need to send to the server should be string';
         $.ajax({
                  type: "POST",
                  url: "pagename.aspx/Save",
                  data: "{param1: '"+ param1 +"' , param2 :'"+ param2 +"'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: false,
                  cache: false,
                  success: function(result){
                 //Successfully gone to the server and returned with the string result of the server side function do what you want with the result  

                  }
                  ,error(er)
                  {
                  //Faild to go to the server alert(er.responseText)
                  }
          });
        }

4- Call this function on the button click

for more questions or descriptions of my code and script i'm here :)

Upvotes: 8

Nasser Hadjloo
Nasser Hadjloo

Reputation: 12630

Ypu have to use Ajax techniques, JQuery, Asp.net, YUI, and the rest of api and libraries that let you use Ajax techniques.

The easiest one in Asp.net is using builtin Asp.net Ajax functionalities by adding a ScriptManager and UpdatePanel to your page

Upvotes: 1

Cris Valenzuela
Cris Valenzuela

Reputation: 372

An easy way is to use a page method or static methods on a page and use jquery to send or get data from the server side code. This link has a good walkthrough

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

Upvotes: 2

Quentin
Quentin

Reputation: 944320

The technique is called Ajax and there are no shortage of tutorials and libraries (not to mention support in the big libraries such as YUI or jQuery).

Upvotes: 1

Related Questions