What event is fired when the asp.net page loads in the browser

I need to run the method on my asp.net page. The condition is that this method should work when the page loads in the client’s browser, and the client can see it. What can I do on the server side and what can I do on the client side?

+3
source share
4 answers

You can use jQuery to determine if a page is loaded very easily on the client side;

$(document).ready(function() 
{
    //page is fully loaded and ready, do stuff here
}

Server side: you can use a combination of WebMethod, JSON and Javascript (AJAX)

Client side concept:

//////INLINE YOUR ASPX PAGE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ConsumeWebService()
{
     SampleService.TehMethod();
}

$(document).ready(function() 
{
    //page is fully loaded and ready, do stuff here
}
</script>
//////////////////

Server side concept:

<%@ WebService Language="C#" Class="SampleService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SampleService: System.Web.Services.WebService
{
    [WebMethod]
    public void TehMethod()
    {
        //do stuff server-side here
    }

}

Here is a more detailed article on the process http://msdn.microsoft.com/en-us/library/bb515101.aspx

+2

, .
, script.

load.

+4

, - UpdatePanel Timer UpdatePanel .

. Timer UpdatePanel

The above solution will load the page and then your client will see that the page is being displayed dynamically. Now, if you really need your client to find out what happens as soon as the page loads, you will need to run the code on the client, which calls the web service on the server, which will run your code.

+4
source

You can also decorate your method as a web method and call it via javascript when the page finishes loading.

+4
source

Source: https://habr.com/ru/post/1792382/


All Articles