繁体   English   中英

Javascript或Jquery将数据从Javascript中定义的html元素发布到ASP.NET,然后获取返回值并显示它

[英]Javascript or Jquery Posting of data to ASP.NET from an html element defined in Javascript then getting the returned value and displaying it

我需要使用.Net 3.5,这是我到目前为止所拥有的

的HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chooseElecDiv.aspx.cs" Inherits="chooseElecDiv" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <meta charset="utf-8"/>
    <title>Choose</title>
    <script src="js/classManipulator.js"></script>
</head>
<body class="body">
    <form id="Form1" runat="server">
        <div class="w-container header">
            <div class="w-section w-clearfix header-section">
                <div class="header-link">Logout</div>
                <div class="header-link">Manage</div>
            </div>

        </div>
        <div class="w-container const-cont">
            <div class="w-section const-section">
                <div id="const-details" class="const-details"></div>
            </div>

        </div>
        <div class="w-container selection-cont">
            <div class="w-section selection-section">
                <div class="w-row selection-row">
                    <div id="opts-sel" class="w-col w-col-3 selected-col">

                    </div>
                    <div id="sel-opts" class="w-col w-col-9 options-col">

                    </div>

                </div>

            </div>

        </div>
        <script type="text/javascript" src="js/jquery.min.js"></script>            
    </form>
</body>

Javascript:位于js / classManipulator.js中

将值作为json发布到ASP.NET

function saveAddresses() {
$.ajax({
    type: "POST",
    url: "chooseElecDiv.aspx/saveAddresses",
    data: "{}",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: OnSuccess,
    error: OnError
});
}
function OnSuccess(data) {
    alert(data);
}
function OnError(data) {
    alert(data);
}


window.onload = function(){
    var form = document.createElement("form");
    addMyClass(form, "ps-form");
    form.setAttribute("data-name", "Email Form");
    form.id = "email-form";
    form.name = "email-form";
    document.getElementById("const-details").appendChild(form);

    /*THIS CREATES THE BUTTON AND ADDS IT TO A PREVIOUSLY DEFINED FORM*/
    var saveButton = document.createElement("input");
    saveButton.setAttribute("data-wait", "Please wait...");
    saveButton.setAttribute("runat", "server");
    saveButton.type = "button";
    saveButton.value = "Save";
    saveButton.id = "savebtn";
    form.appendChild(saveButton);
    saveButton.addEventListener("click", saveAddresses);
}

ASP.NET

using System;
using System.Collections.Specialized;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;


namespace ASPLoadDataJquery
{ 
    [MetadataType(typeof(chooseElecDiv))]
    public partial class chooseElecDiv : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string SaveAddresses()
    {
        System.Threading.Thread.Sleep(5000);
        return "Hello tks";
    }

}

}

我希望能够将json数据发送到ASP.NET,并具有ASP.NET进程并通过响应确认接收。

如何使C#在不使用定义为ASP.NET对象的元素的情况下直接从按钮检测发布请求,并直接使用后面的代码对其进行处理?

如何获取ASP.NET来接收发布为json的数据?

我得到错误

Compiler Error Message: ASPNET: Make sure that the class defined in
this code file matches the 'inherits' attribute, and that it extends
the correct base class (e.g. Page or UserControl).

Source Error:
Line 18: { 
Line 19:     [MetadataType(typeof(chooseElecDiv))]
Line 20:     public partial class chooseElecDiv : System.Web.UI.Page

您的问题出在aspx页面标题中。 应该

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chooseElecDiv.aspx.cs" CodeFileBaseClass="ASPLoadDataJquery.chooseElecDiv" Inherits="ASPLoadDataJquery.chooseElecDiv" %>

至于后端的ajax调用,您将处于正确的轨道[WebMethod]是关键。

实际上,写页面标题标签的首选方法是。

<%@ Page Language="C#" AutoEventWireup="true" Inherits="ASPLoadDataJquery.chooseElecDiv" Codebehind="chooseElecDiv.aspx.cs" %>

我想这个Web应用程序是从某个网站转换而来的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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