简体   繁体   中英

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.

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

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/

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.

该技术称为Ajax ,不乏教程 (更不用说对大库(如YUIjQuery )的支持)。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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