我正在学习如何在asp.NET和jQuery
上使用ajax,并试图在浏览器中模拟简单的文件编辑器。到目前为止,我有一个使用ajax和jQuery
通过WebMethods加载的文件列表。当我尝试复制该功能以点击按钮以加载文件时,我得到一个无效回传的例外。我试图链接jQuery
到按钮和调试传入值的许多方法,但它已经不早,我似乎根本无法找到问题d:ASP.NET无效“asp:button”上的回发或回调参数点击JQuery POST
笔者认为:
<form id="textform" runat="server">
<div>
<asp:DropDownList ID="ddlSelectFile" runat="server">
<asp:ListItem Text="Select a file..." Value="" />
</asp:DropDownList>
<asp:Button ID="btnLoadFile" runat="server" Text="Load File" />
<asp:Button ID="btnSaveFile" runat="server" Text="Save File" />
<br />
<asp:TextBox ID="txtFileEditor" runat="server" TextMode="MultiLine" Width="100%" Height="100%" />
</div>
</form>
代码隐藏:
public partial class WebEditor : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<string> GetFileList()
{
string path = HostingEnvironment.ApplicationPhysicalPath + "\\Files";
List<string> files = new List<string>();
foreach(string filePath in Directory.GetFiles(path))
{
string fileName = Path.GetFileName(filePath);
files.Add(fileName);
}
return files;
}
[WebMethod]
public static string LoadFileContents(string fileName)
{
string fileContents = string.Empty;
string path = HostingEnvironment.ApplicationPhysicalPath + "\\Files\\" + fileName;
if (File.Exists(path))
{
fileContents = File.ReadAllText(path);
}
return fileContents;
}
}
FileEditor.js:
$(document).ready(function() {
$("#btnLoadFile").on('click', LoadFile);
$.ajax({
type: "POST",
url: "WebEditor.aspx/GetFileList",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnFileListReceived,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
function OnFileListReceived(response) {
var files = response.d;
var fileList = $("#ddlSelectFile");
$(files).each(function (val, text) {
fileList.append($('<option></option>').val(text).html(text));
});
}
});
function LoadFile() {
var selectedFile = $("#ddlSelectFile").val();
$.ajax({
type: "POST",
url: "WebEditor.aspx/GetFileList",
data: JSON.stringify({ selectedFile: selectedFile }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnFileContentsReceived,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
function OnFileContentsReceived(response) {
var fileContents = response.d;
}
的excep引发的信息:
[ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +9748914
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +108
System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +64
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +18
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +457
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1833
对这个问题的任何建议/修复将是美好的。
在按钮单击中,您似乎在调用WebEditor.aspx/GetFileList的web方法,它没有任何参数,但是您将参数传递给data:JSON.stringify({selectedFile :selectedFile})'你的第一个调用没有这样的参数'data:'{}'''。你为什么需要这个? – Aruna
你可以看看我下面更新的答案。 – Aruna