[英]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.