Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to execute javascript before Page load in ASP.NET application.

My function returns user location, and I would like to pass value to server side and load data based on location.

The problem is javascript function executes after page load.

Here is my code:

    $(function () {
        getLocation();
    });

in getLocation function I set hidden field value

$("#<%= HfLocation.ClientID %>").val(location);

in code behind I try to get value but it's always empty

protected void Page_Load(object sender, EventArgs e)
        {
            var location = HfLocation.Value;
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

I need to execute javascript before Page load in ASP.NET application

This requirements makes no sense. Remember how ASP.NET works:

  1. A user request hits the web server
  2. The web server dispatches the request to ASP.NET engine.
  3. The ASP.NET engine instantiates the page and goes through the entire page lifecycle.
  4. The page is rendered as HTML and is sent to the client
  5. The client browser builds the DOM, runs client side javascript, ...

You see that it is impossible to have step 5 execute before step 3 (in which the Page_Load event executes).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...