哪一个的Page_Load(对象发件人,EventArgs e)和$(文件)。就绪(函数()之间首先运行? [英] Which one is run first between Page_Load(object sender, EventArgs e) and $(document).ready(function ()?

查看:116
本文介绍了哪一个的Page_Load(对象发件人,EventArgs e)和$(文件)。就绪(函数()之间首先运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了jQuery我的asp.net网页。我无法理解compleletly运行秩序。所以我写的foolowing code进行测试。在任何情况下,都会的Page_Load首先运行超过$(文件)。就绪(),是不是?

 <%@页面语言=C#AutoEventWireup =真codeFILE =Default2.aspx.cs继承=默认设置2%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    <脚本SRC =JS / jQuery的-1.7.1.min.js类型=文/ JavaScript的>< / SCRIPT>    <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
            警报('地狱');
        });
    < / SCRIPT>< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:标签ID =Label1的=服务器文本=标签>< / ASP:标签>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;公共部分类默认2:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            Label1.Text =世界;
        }
    }
}


解决方案

的Page_Load 是一个服务器端事件。 $(文件)。就绪()是客户端。

该渲染页面的服务器的事件总是会先运行。服务器的事件可响应于异步回发运行附加倍,但初始渲染将总是从服务器发生到客户端,而不是周围的其他方法。

有在两个事件之间没有直接的关系,即一个并不真正需要的其他的存在的存在。你可以写一个ASP.NET网页,其中呈现的内容,以二进制流(因而没有任何文档事件),或者你可以写一个普通的HTML网页,其中有没有服务器code(没有的OnLoad方法)。

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_Load is 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).

这篇关于哪一个的Page_Load(对象发件人,EventArgs e)和$(文件)。就绪(函数()之间首先运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆