简体   繁体   English

javascript pageLoad函数没有在asp.net Site.Master中触发

[英]javascript pageLoad function not firing in asp.net Site.Master

I am trying to get this javascript function to fire every time there is a postback in my ASP.net project using c#. 我试图在每次使用c#在我的ASP.net项目中回发时启动这个javascript函数。 I read online that if you create a pageLoad() function in Javascript, it will fire every time on a postback. 我在网上看到,如果你在Javascript中创建一个pageLoad()函数,它每次都会在回发时触发。 I can't get it to fire. 我无法解雇它。 Here is all the code in my Site.Master file. 这是我的Site.Master文件中的所有代码。

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Weights.SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="<%=ResolveUrl("~/Scripts/jquery-1.4.1.min.js")%>"></script>
        <script type="text/javascript">
            function pageLoad() {
                if ($('#MainContent_ckbIncludeWeight').checked) {
                    $('#MainContent_txtPalletWeight').show();
                    console.log('it is checked!');
                    alert('it is checked!');
                }
                if (true) {
                    console.log('fire!');
                    alert('fire!');
                }
            }
        </script>
        <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
        <asp:ContentPlaceHolder ID="HeadContent" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form runat="server">
        <div class="page">
            <div class="header">
                <div class="title">
                    <h1>
                        My ASP.NET Application
                    </h1>
                </div>
                <div class="loginDisplay">
                    <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                        <AnonymousTemplate>
                            [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                            [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
                        </LoggedInTemplate>
                    </asp:LoginView>
                </div>
                <div class="clear hideSkiplink">
                    <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                        <Items>
                            <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                            <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
                        </Items>
                    </asp:Menu>
                </div>
            </div>
            <div class="main">
                <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="footer">

        </div>
        </form>
    </body>
    <script type="text/javascript">
        function pageLoad() {
            if ($('#MainContent_ckbIncludeWeight').checked) {
                $('#MainContent_txtPalletWeight').show();
                console.log('it is checked!');
                alert('it is checked!');
            }
            if (true) {
                console.log('fire!');
                alert('fire!');
            }
        }
        $(document).ready(function () {

            $('#MainContent_ckbIncludeWeight').click(function () {
                if (this.checked) {
                    $('#MainContent_txtPalletWeight').show();
                    $('#MainContent_txtPalletWeight').val('40');
                    $('#MainContent_txtPalletWeight').after("<span id='pound'>#</span>");
                }
                else {
                    $('#MainContent_txtPalletWeight').hide();
                    $('#MainContent_txtPalletWeight').val('');
                    $('#pound').remove();
                }
            });
        });
    </script>
</html>

I would appreciate any help. 我将不胜感激任何帮助。 Thanks Mike 谢谢迈克

You're already using jQuery's $(document).ready , just add your code there as well. 你已经在使用jQuery的$(document).ready ,只需在那里添加你的代码。 It will run every time page reloads: 每次页面重新加载时它都会运行:

    $(document).ready(function () {

        $('#MainContent_ckbIncludeWeight').click(function () {
            if (this.checked) {
                $('#MainContent_txtPalletWeight').show();
                $('#MainContent_txtPalletWeight').val('40');
                $('#MainContent_txtPalletWeight').after("<span id='pound'>#</span>");
            }
            else {
                $('#MainContent_txtPalletWeight').hide();
                $('#MainContent_txtPalletWeight').val('');
                $('#pound').remove();
            }
        });


        // Originally from function pageLoad()
        if ($('#MainContent_ckbIncludeWeight').checked) {
            $('#MainContent_txtPalletWeight').show();
            console.log('it is checked!');
            alert('it is checked!');
        }
        if (true) {
            console.log('fire!');
            alert('fire!');
        }


    });

You have to have ScriptManager on page in order for pageLoad() to work. 您必须在页面上安装ScriptManager才能使pageLoad()正常工作。 There is however no need for adding it, if you just want to run some script on every postback. 但是,如果您只想在每个回发上运行一些脚本,则无需添加它。

You can do something like putting this: 你可以做这样的事情:

<asp:PlaceHolder ID="phPageLoad" runat="server">
  <script type="text/javascript">
        if ($('#MainContent_ckbIncludeWeight').checked) {
            $('#MainContent_txtPalletWeight').show();
            console.log('it is checked!');
            alert('it is checked!');
        }
        if (true) {
            console.log('fire!');
            alert('fire!');
        }
  </script>
</asp:PlaceHolder>

into your master page and in codebehind of master page add this to Page_Load: 进入您的母版页并在母版页的代码隐藏中将其添加到Page_Load:

phPageLoad.Visible = Page.IsPostback;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM