[英]ASP.NET - Code behind (C#) not being called - AutoEventWireup is set to True
I was working on an ASP.NET page and the code behind was executing properly.我正在处理一个 ASP.NET 页面,后面的代码正在正确执行。 I did more work on it and tried running it a few hours later, and the code stopped executing.我做了更多的工作,几个小时后尝试运行它,代码停止执行。 I don't understand why it's no longer running the C# code.我不明白为什么它不再运行 C# 代码。 The more common code behind problems that I could find on Google/Stack Overflow pointed at the AutoEventWireup being set to false or missing the runat server, but that doesn't seem to be true in my case?我可以在 Google/Stack Overflow 上找到的问题背后更常见的代码指出 AutoEventWireup 被设置为 false 或缺少 runat 服务器,但在我的情况下似乎并非如此? I have a feeling it's a minor mistake, but I can't seem to find it.我有一种感觉,这是一个小错误,但我似乎无法找到它。
Here's the code in the ASP page:这是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>
This is what's in Site.Master.cs:这是 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");
}
}
}
and this is what's in Default.aspx.cs这就是 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();
}
}
}
Nothing is being written to the console debug.控制台调试没有写入任何内容。
From this https://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/从这个https://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/
You can try to remove the "static" in Page_Load() method.您可以尝试删除 Page_Load() 方法中的“静态”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.