1

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.

1
  • 2
    Try to goole for asp.net ajax tutorial. Commented Oct 9, 2011 at 6:55

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure the data line is correct here? I can't understand the logic in your use of " and ' signs. Shouldn't it be something like: data: "'{param1: '+ param1 +' , param2 :'+ param2 +'}'". We just want to concatenate a couple of strings and put " " around the final result.
well i use this sign " as a container of the stringfyed JSON Object its up to you when you put the values if the value is a String Type you should deal with it as string so we put the ' sign
so the resulting data string will look like: {param1: 'blah', param2: 'blahblah'}?
2

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/

Comments

1

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

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.