如何将值从 javascript 发送到服务器端(asp.net)?

发布于 2024-12-08 15:19:22 字数 99 浏览 0 评论 0 原文

在不刷新页面的情况下将值从 JavaScript(客户端)发送到服务器(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百合的盛世恋 2024-12-15 15:19:22

简单的方法:

1-将jquery导入到您的页面
2- 在页面的 cs 文件中创建您的函数,如下所示

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

3- 在您的 aspx 页面中创建您的函数

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- 在按钮上调用此函数单击

以获取更多问题或我的代码和脚本的说明我在这里:)

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

五里雾 2024-12-15 15:19:22

一种简单的方法是在页面上使用页面方法或静态方法,并使用jquery从服务器端代码发送或获取数据。此链接有一个很好的演练

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

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/

万劫不复 2024-12-15 15:19:22

Ypu必须使用Ajax技术,JQueryAsp.netYUI,以及让您使用 Ajax 技术的其余 api 和库。

Asp.net 中最简单的方法是通过向页面添加 ScriptManagerUpdatePanel 来使用内置的 Asp.net Ajax 功能

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

剑心龙吟 2024-12-15 15:19:22

该技术称为 Ajax 并且不乏 教程(更不用说大型库的支持,例如 YUI< /a> 或 jQuery)。

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

新雨望断虹 2024-12-15 15:19:22

没有人真正回答这个问题。因为,“导入jquery”“使用ajax”等都否定了OP要求javascript解决方案的事实。这是 JavaScript 中的一句话/非常容易做到。

在你的 JavaScript 中,你只需调用该方法:

PageMethods.SomeMethod('test');

你的“SomeMethod”将是一个代码隐藏方法,如下所示:

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

规则:
您必须使用 WebMethod 属性来标识您的代码隐藏方法。它必须是静态的。并且您必须在页面中注册一个脚本管理器,如下所示:

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

由于我正在使用 aspx webforms 页面来执行一些非常简单的 javascript 功能,例如检索/隐藏地理位置,因此我根据需要将其放在 Form 元素内。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文