Page_Load of 2. 用户控件从不执行(网站)

Page_Load of 2. user control never executes (website)

我正在制作一个在框中显示信息的网站。 不同的页面会有不同的框组合。他们会有很多共同的盒子。 因此,我将这些框作为用户控件,然后放在不同的页面上。不同的页面也将共享一个母版页。 但是,在开始开发2.box的时候,我运行陷入了一个问题:

1. 框 运行 的 Page_Load(或后面的整个代码)很好,但是 2. 框的 Page_Load 被忽略了。这会导致大量空值,从而导致 aspx 文件崩溃。

我的 CT1.aspx 包含这个以使用两个用户控件:

<%@ Page Title="CustType1" Language="C#" MasterPageFile="~/MTCustomer/Master.master" AutoEventWireup="true" CodeFile="CT1.aspx.cs" Inherits="MTCustomer.CT1" %>

// Some HTML
    <td ID="column1" style="width: 400px;">
        <Box:CustomerInfoBoxAx runat="server" />
        <Box:CustomerInfoBoxMt runat="server" />
    </td>
// Some HTML

我的 Web.config 包含此用户控件可用:

<pages>
  <controls>
    <add tagPrefix="Box" tagName="InfoBoxA" src="~/Controls/InfoBoxA.ascx" />
    <add tagPrefix="Box" tagName="InfoBoxB" src="~/Controls/InfoBoxB.ascx" />
  </controls>
</pages>

我的用户控件 A 看起来像这样:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InfoBoxA.ascx.cs" Inherits="Controls.InfoBoxA" %>

<% if (IsVisible("InfoBoxA") && Customer != null)
   { %>
    <div>
        <%-- Markup --%>
    </div>
<% } %>

盒子 B 看起来像这样:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InfoBoxB.ascx.cs" Inherits="Controls.InfoBoxB" %>

<link rel="stylesheet" type="text/css" href="<%=VirtualPathUtility.ToAbsolute("~/Styles/InfoBoxStyle.css")%>">

<% if (IsVisible("InfoBoxB") && Customer != null)
   { %>
    <div>
        <%-- Markup --%>
    </div>
<% } %>

我曾尝试交换 page.aspx 中两个用户控件的顺序,这也改变了 一个 Page_Load (或后面的整个代码)被忽略。基于此,似乎只执行了后面的第一段代码,其余代码被忽略。

编辑: 在跟踪初始空引用异常之后出现的异常之后,我可以看到第一个用户控件中的 Page.DataBind() 仍在执行。这一定意味着这就是导致框一和框二的 aspx 文件的原因 运行。但我该如何解决这个问题?

问题已解决。 我从所有用户控件中删除了 Page.DataBind(),并在 CT1.aspx.cs(后面的页面代码)中将其从 Page_Load() 移到了 OnLoadComplete()

通过这样做,Page_Load()中的所有文件都在Page.DataBind()执行之前执行了。