JS没有点击asp:Button

时间:2014-06-02 07:28:44

标签: javascript asp.net ajax json onclick

我在JS中有一个数组,我正在使用以下函数将其发送到aspx页面

var array = [] //I want this array to be sent with JSON.
function result()
{
    var jsonText = JSON.stringify({ list: array });

    $.ajax({
        type: "POST",
        url: "test.aspx",
        data: jsonText,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () { alert("it worked"); },
        failure: function () { alert("Uh oh"); }
    });

}

我在点击asp中的按钮时调用它,就像这样:

<asp:Button id="submitbtn" runat="server" OnClick="Button1_Click" OnClientClick="result()" /> 

但它不起作用。那么,确切的方法是什么呢?

1 个答案:

答案 0 :(得分:1)

请尝试下面适合您的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demotest.aspx.cs" Inherits="Web.demotest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
        var array =[[48.154176701412744,11.551694869995117],[48.15131361676726,11.551694869995117],[48.15555092529958,11.549291610717773]]
    function result() {
        var jsonText = JSON.stringify({ list: array });
        $.ajax({
            url: "demotest.aspx/Demo", type: "POST", dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: jsonText,
            success: function (data) { alert("it worked"); },
            error: function () { alert("Uh oh"); }
        });
        return false;
    }

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button id="submitbtn" runat="server" Text="Ajax Call"  OnClientClick="javascript:return result();" /> 
    </div>
    </form>
</body>
</html>

在C#中写下以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace Web
{
    public partial class demotest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static void Demo(double[][] list)
        {
            //Write code here
        }
    }
}

对于在c#中使用的数据类型。请参见下图 enter image description here