2013-11-01 29 views
3

我試圖讓這個javascript函數火每次有在使用C#我的ASP.net項目回傳時間。我在線閱讀,如果你在Javascript中創建一個pageLoad()函數,它會在每次回發時觸發。我無法啓動它。以下是我的Site.Master文件中的所有代碼。的JavaScript pageLoad函數中不費一槍在asp.net的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> 

我將不勝感激任何幫助。 謝謝 Mike

+0

也許是這樣的:http://stackoverflow.com/questions/9654808/do-something-javascript-before-asp-net-anypostback – bump

回答

2

您已經在使用jQuery的$(document).ready,只是在那裏添加你的代碼。它將運行每次重新加載頁面:

$(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!'); 
     } 


    }); 
+0

我已經嘗試過它的document.ready,但研究後,我讀了說,在的document.ready代碼不會對火回傳,僅在初始頁面加載的文章。 http://encosia.com/document-ready-and-pageload-are-not-the-same/ – dmikester1

+0

是您的頁面通過的UpdatePanel做完全回發或部分回發? –

+0

不確定UpdatePanel是什麼,但我在想全回發。 – dmikester1

0

爲了使pageLoad()正常工作,必須在頁面上有ScriptManager。 但是,如果您只是想在每次回發中運行一些腳本,則無需添加它。

你可以做這樣的事情把這個:

<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> 

到您的母版頁和母版頁的代碼隱藏它添加到Page_Load中:

phPageLoad.Visible = Page.IsPostback; 
+0

我這樣做,並在代碼隱藏(我假設是Site.Master.cs)我得到這個錯誤,當我再補充一點行錯誤:名稱「phPageLoad」不會在目前情況下 – dmikester1

+0

存在,那麼phPageLoad不會自動生成的Site.Master。無論出於何種原因,designer.cs;檢查佔位符是否位於

標記內,如果它不起作用,請在Site.Master.cs中明確聲明它,如受保護的PlaceHolder phPageLoad; –

相關問題