简体   繁体   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

I am required to use .Net 3.5 and This is what I have so far 我需要使用.Net 3.5,这是我到目前为止所拥有的

HTML 的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: located in js/classManipulator.js Javascript:位于js / classManipulator.js中

Posts the value as json to ASP.NET 将值作为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 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";
    }

}

} }

I would like to be able to send the json data to ASP.NET and have ASP.NET process and confirm receipt by responding. 我希望能够将json数据发送到ASP.NET,并具有ASP.NET进程并通过响应确认接收。

How do I get C# to detect the post request from the button without using elements defined as ASP.NET objects and processing them directly with the code behind? 如何使C#在不使用定义为ASP.NET对象的元素的情况下直接从按钮检测发布请求,并直接使用后面的代码对其进行处理?

How do I get ASP.NET to receive the data that is posted as json? 如何获取ASP.NET来接收发布为json的数据?

I get error 我得到错误

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

Your problem is in the aspx page header. 您的问题出在aspx页面标题中。 Should be 应该

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

As for ajax calls to the back end you are on the right track [WebMethod] is the key. 至于后端的ajax调用,您将处于正确的轨道[WebMethod]是关键。

Preferred way to write the page header tag would actually be. 实际上,写页面标题标签的首选方法是。

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

Im guessing this web application was converted from a web site at some point. 我想这个Web应用程序是从某个网站转换而来的。

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

相关问题 ASP.NET MVC / Jquery Unobtrusive Ajax,在返回html数据后尝试检查元素是否加载了Jquery - ASP.NET MVC / Jquery Unobtrusive Ajax, trying to check if element is loaded with Jquery after the html data is returned 使用JavaScript和ViewModel在Asp.NET MVC 5应用程序中发布数据 - Posting Data in Asp.NET MVC 5 app with JavaScript & ViewModel 在 javascript 中获取 ASP.NET session 值? - Getting ASP.NET session value in javascript? 从AJAX中的javascript向C#方法发布请求,无返回值,ASP.NET,MVC - Posting request from javascript in AJAX to C# methods, no returning value, ASP.NET, MVC ASP.Net Ajax-JavaScript:如何在HTML中使用返回的变量显示 - ASP.Net Ajax - JavaScript: How to display with returned variable in HTML Javascript、Razor、Asp.Net Core:未定义 View.Element - Javascript, Razor, Asp.Net Core: View.Element is not defined 无法在JavaScript中附加从asp.net网络服务返回的数组值 - Can't append the array value returned from asp.net webservice in javascript 从asp.net向JavaScript传递值 - Passing value to javascript from asp.net 如何通过jQuery / javascript在ASP.NET转发器中选择特定的html元素? - How to select a specific html element in an ASP.NET repeater through jQuery/javascript? 如何用div替换表单元素并将内部html保留在jQuery或asp.net javascript lib中? - how to replace form element with div and keep the inner html in jQuery or asp.net javascript lib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM