gpt4 book ai didi

javascript - 在 asp.net 中使用 javascript 填充国家和城市下拉列表

转载 作者:行者123 更新时间:2023-12-03 08:59:19 27 4
gpt4 key购买 nike

我在花式框中有一个注册表单作为内联内容,并且可以在所有站点中访问它,因为链接位于母版页中。

在注册表单上,我有两个下拉列表,一个用于国家,另一个用于城市。当用户更改国家/地区时,城市下拉列表会刷新为先前选择的国家/地区。来自国家和城市的所有数据我从脚本中获得https://bdhacker.wordpress.com/tag/all-countries-of-the-world/

问题是,当用户提交表单时,Firefox 中会出现错误提示

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.

Description: 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.

Exception Details: 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.

正如错误指出的那样,我必须设置 enableeventvalidation=false。问题是我必须在每个页面上都这样做,因为fancybox 在整个站点中,而且我读到它不是最佳安全实践。当异常抛出时,其他选项是使用两个下拉列表的客户端脚本管理器注册每个选项,这会很烦人,因为有 200 多个国家和 10.000 个城市!

有什么想法我能做什么?

这是一些代码
<%@ Master Language="C#" AutoEventWireup="True" CodeBehind="Site.master.cs" Inherits="VozDirecta.SiteMaster" %>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$("#RegistroLightbox").fancybox({

});
});

</script>

<body id="page1">
<form id="Form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
<a class="labelsTipolinks" id="RegistroLightbox" href="#LightBoxRegistroDiv">Registro</a>

<div style="display: none">
<div id="LightBoxRegistroDiv">
<asp:DropDownList ValidationGroup="grupoValidacionRegistroUsuario" runat="server"
ID="drpDownPais" CssClass="textoLightbox" AutoPostBack="false" CausesValidation="false">
</asp:DropDownList>
<asp:DropDownList ValidationGroup="grupoValidacionRegistroUsuario" runat="server"
ID="drpDownCiudad" CssClass="textoLightbox" AutoPostBack="false" CausesValidation="false">
</asp:DropDownList>
</div>
</div>

我考虑其他选择从数据库中获取数据并绑定(bind)到下拉列表,但我宁愿留在 javascript

最佳答案

我最终使用了纯 html 选择控件并将两个值保存在两个隐藏字段中。有了这个,我可以使用默认值为 true 来保留 enableeventvalidation :)

这里有一些代码:

<select CssClass="textoLightbox" id="selectPais" onchange="cambiarCiudad()"></select>
<asp:HiddenField runat="server" ID="hdnPaisSeleccionado" />

<select CssClass="textoLightbox" id="selectCiudad" onchange="guardarCiudad()"></select>
<asp:HiddenField runat="server" ID="hdnCiudadSeleccionada" />


<script language="javascript" type="text/javascript">
$(document).ready(function () {
ImprimirPais("selectPais");
var dropPais = document.getElementById("selectPais");
var dropCiudad = document.getElementById("selectCiudad");
dropPais.value = "USA";
print_state('selectCiudad', dropPais.selectedIndex)
dropCiudad.value = "California";
document.getElementById("<%=hdnPaisSeleccionado.ClientID%>").value = "USA";
document.getElementById("<%=hdnCiudadSeleccionada.ClientID%>").value = "California";

});

//Repopula el combo de las ciudades cuando es cambiado el pais.
function cambiarCiudad() {
var dropPais = document.getElementById("selectPais");
//Vacia ciudad
document.getElementById('selectCiudad').options.length = 0;
//Repopula ciudad de acuerdo al pais
print_state('selectCiudad', dropPais.selectedIndex);
//Guarda Pais
document.getElementById("<%=hdnPaisSeleccionado.ClientID%>").value = dropPais.value;
//Guarda Ciudad
guardarCiudad();
}

function guardarCiudad() {
var dropCiudad = document.getElementById("selectCiudad");
document.getElementById("<%=hdnCiudadSeleccionada.ClientID%>").value = dropCiudad.value;
}

关于javascript - 在 asp.net 中使用 javascript 填充国家和城市下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com