簡體   English   中英

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

[英]Populate countries and cities dropdownlist with javascript in asp.net

我在 fancybox 中有一個注冊表單作為內聯內容,並且可以在所有站點中訪問它,因為鏈接位於母版頁中。

在注冊表單上,我有兩個下拉列表,一個用於國家,另一個用於城市。 當用戶更改國家/地區時,城市下拉列表會刷新為先前選擇的國家/地區。 我從腳本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 在整個站點中,而且我讀到它不是最佳安全實踐。 其他選項是使用,作為異常拋出,注冊每個選項與 clientscriptmanager 的兩個下拉列表,這將是令人厭煩的,因為有超過 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>

我考慮其他選項來從數據庫中獲取數據並綁定到下拉列表,但我寧願使用 javascript

我最終使用純 html 選擇控件並將兩個值保存在兩個 hiddenFields 中。 有了這個,我可以保留默認值為 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM