我创建了一个简单的示例,它使用ajax post方法调用WebMethod并从服务器端检索数据。除SelectedIndexChanged
事件外,一切正常。
错误说:
说明: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
例外详细信息: System.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.
我还阅读了一些 solutions 但是,我不想使用更新面板,也不想在服务器端填充。作为 norbertB 已经建议禁用事件验证是一个坏主意,因为你失去了一点安全性,成本很低
我知道当我从客户端填充下拉列表时,viewstate不会更新。那么,我该怎么做才能解决这个问题呢?
我的代码很简单
服务器端
<Services.WebMethod>
Public Shared Function getDDL() As ArrayList
Dim arr As New ArrayList
arr.Add(New DDLCls(1, "Nimesh"))
arr.Add(New DDLCls(2, "Rahul"))
arr.Add(New DDLCls(3, "Hiren"))
arr.Add(New DDLCls(2, "Dipak"))
Return arr
End Function
客户端
$.ajax({
type: 'POST',
url: 'Default.aspx/getDDL',
data: '{}',
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (data) {
$.each(data.d, function (index, item) {
$('#ddl').get(0).options[$('#ddl').get(0).options.length] = new Option(item.Display, item.Value);
});
},
error: function (data) {
alert('Error on web method call: ' + data);
}
}
);
ASP.NET代码
<asp:DropDownList ID="ddl" runat="server" AutoPostBack ="true" EnableViewState="false"></asp:DropDownList>
<input type="button" id="btnDate" onclick="getDate()" value="Get Date" />
我的问题是“在客户端填充SelectedIndexChanged
时是否可以获得DropDownList
个事件?”