gpt4 book ai didi

c# - javascript pageLoad 函数未在 asp.net Site.Master 中触发

转载 作者:行者123 更新时间:2023-11-29 14:54:49 25 4
gpt4 key购买 nike

每次在我的 ASP.net 项目中使用 c# 进行回发时,我都试图让这个 javascript 函数触发。我在网上读到,如果你在 Javascript 中创建一个 pageLoad() 函数,它会在每次回发时触发。我不能让它开火。这是我的 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>

如果有任何帮助,我将不胜感激。谢谢迈克

最佳答案

您已经在使用 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!');
}


});

关于c# - javascript pageLoad 函数未在 asp.net Site.Master 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com