繁体   English   中英

ASP.NET - 未调用隐藏代码 (C#) - AutoEventWireup 设置为 True

[英]ASP.NET - Code behind (C#) not being called - AutoEventWireup is set to True

我正在处理一个 ASP.NET 页面,后面的代码正在正确执行。 我做了更多的工作,几个小时后尝试运行它,代码停止执行。 我不明白为什么它不再运行 C# 代码。 我可以在 Google/Stack Overflow 上找到的问题背后更常见的代码指出 AutoEventWireup 被设置为 false 或缺少 runat 服务器,但在我的情况下似乎并非如此? 我有一种感觉,这是一个小错误,但我似乎无法找到它。

这是ASP页面中的代码:

<%@ Page Title="Test Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WritebackModel.WritebackModel" Runat="server"%>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <script type="text/javascript">

        function setText()
        {
            <%if (successfulWriteback) {%>
                document.getElementById('resultTitle').innerHTML = "Success!";
                document.getElementById('resultBody').innerHTML = "An exception was successfully added!";
            <%} else { %>
                document.getElementById('resultTitle').innerHTML = "Invalid Permissions";
                document.getElementById('resultBody').innerHTML = "Please contact your payroll rep to add a missing work record exception.";
            <%}%>
            <%Console.WriteLine($"Writeback: {successfulWriteback}");%>
        }

        function countdown()
        {
            var countdownTime = document.getElementById('counter');

            countdownTime.innerHTML = parseInt(countdownTime.innerHTML) - 1;

            if (parseInt(countdownTime.innerHTML) <= 0)
            { 
                window.close();
            }
        }

        function manualClose()
        {
            window.close();
        }

        setInterval(function () { countdown(); }, 1000);

        window.onload = setText;

    </script>

    <div class="jumbotron">
        <h1><span id="resultTitle" style="color:#6a67a7;"> </span></h1>
        <p class="lead" style="color:#a3a1c8;"> <span id="resultBody"> </span></p>
        <p class="lead">This window will close automatically within <span id="counter" style="background-color: #694e7b; color: #fff; display: inline-block; padding: 3px 10px; font-weight: bold; border-radius: 5px;">120</span> second(s).</p>
        <p><a onclick="manualClose()" class="btn btn-primary btn-lg">Close now</a></p>
    </div>

</asp:Content>

这是 Site.Master.cs 中的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WritebackModel
{
    public partial class SiteMaster : MasterPage
    {
        protected static void Page_Load(object sender, EventArgs e)
        {
            WritebackModel.Page_Load(sender, e);
            Debug.WriteLine("Called WritebackModel.Page_Load");
        }
    }
}

这就是 Default.aspx.cs 中的内容

namespace WritebackModel
{
    public partial class WritebackModel : Page
    {
        ...
        public static void Page_Load(object sender, EventArgs e)
        {
            /* Call the appropriate function for the page */
            Debug.WriteLine("Called Pageload function");
            Writeback_MissingWork();
        }
    }
}

控制台调试没有写入任何内容。

从这个https://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/

您可以尝试删除 Page_Load() 方法中的“静态”。

暂无
暂无

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

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