【发布时间】: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 请求发送到.asmxWeb 服务或 WCF 服务 (.svc) 会更好更容易。两者都受 .net 3.5 支持。
标签: javascript c# jquery asp.net json