I add JQuery to my asp.net webpage. I can’t understand the run order compleletly. So I write the foolowing code to test it. In any case, Page_Load will run first than $(document).ready(), is it right?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 src="Js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
alert('Hell');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = "World";
}
}
}
Page_Loadis a server-side event.$(document).ready()is client-side.The server events which render the page will always run first. The server events may run additional times in response to asynchronous postbacks, but the initial rendering will always happen from the server to the client, not the other way around.
There is no direct relationship between the two events, i.e. the existence of one doesn’t really require the existence of the other. You could write an ASP.NET page which rendered content to a binary stream (and thus had no document events), or you could write a plain HTML page which had no server code (and no OnLoad method).