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

StackOverflow https://stackoverflow.com/questions/16119358

  •  11-04-2022
  •  | 
  •  

Question

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>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top