user1946932 user1946932 - 2 months ago 8
Vb.net Question

Vb asp.net JavaScript before on server side page load?

Currently I have strClsCafeListHTML=fGetCafeList() in the Page Load event in a CafesNearMe.aspx.vb file.

This displays a list of Cafes in the page middle section when the page is opened.

How do I delay that from firing until the JavaScript similar to http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation has put the latitude and longitude values into 2 hidden fields, HiddenFieldCurrentLat and HiddenFieldCurrentLong?

(I want fGetCafesList() to inspect those two hidden fields so that the backend SQL can filter for <10 miles.)

Answer

The VB Page_Load event will always happen before the Javascript begins. You could do a workaround, like put your strClsCafeListHTML=fGetCafeList() on a button click event, then trigger that button click via JavaScript after the hiddenfields are populated. If using Jquery, you would do something along the lines of this:

Jquery

$(document).ready(function() {
   //populate hidden fields etc
   $('#myButton').trigger("click");
)};

HTML

<asp:Button runat="server" id="myButton" clientIDMode="static" style="display: none;" onClick="myButton_click"></asp:Button>

VB Code-behind

Protected Sub myButton_Click(sender As Object, e As EventArgs)

    'Do code behind stuff
    'strClsCafeListHTML=fGetCafeList()

End Sub

The concern here is that the button click event will fire another postback, however this will always be the case when running server-side code. If the reloading of the page is an issue, you could try using an <asp:UpdatePanel> or go down the road of AJAX.