how to get google places autocomplete to work if the textbox is initially hidden and should be shown only after a postback

筅森魡賤 提交于 2020-01-23 17:45:35

问题


I've got two textboxes for which I need to use google places autocomplete. These textboxes are contained in a panel which is hidden on page load. There is a list of options from which to select, and once that user input is obtained, the hidden panel should get shown. I've tried both

    Panel.visible = false; 

and

    Panel.Style["display"] = "none";
    Panel.Style["visibility"] = "hidden";

But neither work. Once the panel is hidden the autocomplete for textboxes stop working. I cannot show the panel initially. Is there a work around for this? Can i trigger the autocomplete after a specific postback? Or any other way? Here's the javascript i'm using for autocomplete

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
    var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(7.623887, 68.994141),
    new google.maps.LatLng(37.020098, 97.470703));
    var input1 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality1');
    var input2 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality2');
    var options = {
        bounds: defaultBounds,
        types: ['geocode'],
        componentRestrictions: { country: "IN" }
    };
    autocomplete1 = new google.maps.places.Autocomplete(input1, options);
    autocomplete2 = new google.maps.places.Autocomplete(input2, options);

</script>

回答1:


I finally solved my problem... Found the answer here : https://stackoverflow.com/a/8851767/972821

I kinda realized that i had to reinitialize the javascript after postback...but wasnt sure how it was done... Thanks Aristos. Here's my modified code :

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    function InitializeRequest(sender, args) {
    }

    // fires after the partial update of UpdatePanel
    function EndRequest(sender, args) {
        initialize();
    }
    function initialize() {
        var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(7.623887, 68.994141),
        new google.maps.LatLng(37.020098, 97.470703));

        var input1 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality');
        var input2 = document.getElementById('ctl00_ReportContentPlaceHolder_txtDropLocality');
        var options = {
            bounds: defaultBounds,
            types: ['geocode'],
            componentRestrictions: { country: "IN" }
        };
        autocomplete1 = new google.maps.places.Autocomplete(input1, options);
        autocomplete2 = new google.maps.places.Autocomplete(input2, options);
    }
</script>


来源:https://stackoverflow.com/questions/16119358/how-to-get-google-places-autocomplete-to-work-if-the-textbox-is-initially-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!