【问题标题】:Javascript or Jquery Posting of data to ASP.NET from an html element defined in Javascript then getting the returned value and displaying itJavascript 或 Jquery 从 Javascript 中定义的 html 元素向 ASP.NET 发布数据,然后获取返回值并显示它
【发布时间】:2016-06-16 18:04:19
【问题描述】:

我需要使用 .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

【问题讨论】:

  • ASP.NET Page 类使用自己的数据,即.aspx 页面上的控件。将 AJAX 请求发送到 .asmx Web 服务或 WCF 服务 (.svc) 会更好更容易。两者都受 .net 3.5 支持。

标签: javascript c# jquery asp.net json


【解决方案1】:

您的问题出在 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" %>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2013-03-05
    相关资源
    最近更新 更多