4 ways to detect page load in asp.net.

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 12094 red flag

We will look at four different ways to detect page load event in asp.net

4 ways to detect page load in asp.net.

In this article, we are going to look into fourdifferent ways to detect page load in asp.net application. Sometimes, it is very important to detect page load and in page load event there might few important things to do.

For example, you want to show a welcome or alert message when user will visit in your particular page. In this situation we have to detect page Load event to prompt user.

Among those four techniques first three techniques are client side technique means using Java script or JQuery we will detect page load event and in last example we will see how to use c# to do same. Let’s see one by one.

1) Window.onload property

Window object has onload property and by checking this property we can detect page load in JavaScript. Example code is in below.

<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="ASP.NET.WebForm6"
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        window.onload = function () {
            alert("welcome");
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>
 

2)      Detecting  onLoad() event of <BODY>

 We know HTML pages has two section one is head and other is body. And body has one event called OnLoad(). By checking this onLoad() event we can detect when HTML body is getting load. In below code there is one function in body onLoad() event

<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="ASP.NET.WebForm6"
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function CallLoad() {
            alert("Body OnLoad function");
        }
    </script>
 
</head>
<body onload="CallLoad()">
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

3)      Using JQuery function

We can call document.ready() function of JQuery library and within this function we can write code for body load event.

Don’t forget to include JQuery CDN or local file before use JQuery.

 

<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="ASP.NET.WebForm6"
%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <head runat="server">
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script>
      $(document).ready(function () {
          alert("Using Jquery");
      });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>
 

4) Using C# pageLoad() event

This is server side technique to detect page load in asp.net application. If we write something in Page_Load() event of C# code behind then it will execute at time of page load.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ASP.NET
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("Page Load event");
        }
    }
}
 

Conclusion:

We discussed four techniques to detect page load in asp.net application. If you have different idea please share with us.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)