为什么不是所有控件都在 ASP.NET 页面生命周期的事件处理点初始化?

发布于 2024-08-19 17:16:05 字数 1436 浏览 9 评论 0原文

我在 Web 部件中嵌入了一个用户控件。它具有以下代码片段:(

protected TextBox searchTextBox;
protected Button goButton;
protected Button nextButton;
protected Repeater pushpins;
protected Label currentPageLabel;

protected void goButton_Click(object sender, EventArgs e)
{
 // Do stuff
}

<asp:TextBox ID="searchTextBox" runat="server" />
<asp:Button ID="goButton" runat="server" Text="Go" OnClick="goButton_Click" />
<asp:Repeater ID="pushpins" runat="server" EnableViewState="false">
 <ItemTemplate>Blah</ItemTemplate>
</asp:Repeater>
<asp:Label ID="currentPageLabel" runat="server" />
<asp:Button ID="nextButton" runat="server" Text=" >> " OnClick="nextButton_Click" />

没有 OnLoad 或 CreateChildControls 方法。)

如果我在 goButton_Click 的第一行放置断点,我会发现:

  • searchTextBox: < strong>初始化
  • goButton初始化
  • nextButtonNULL
  • 图钉已初始化
  • currentPageLabel: NULL

为什么有些控件已初始化,而另一些则没有?如果我想更新 currentPageLabel 上的 Text 属性,如何解决此问题?

更新:

我在页面生命周期的整个过程中都放置了断点,发现 nextButtoncurrentPageLabel 从不 已初始化。断点放置在 OnLoad、CreateChildControls、goButton_Click、OnPreRender、Render 和 OnUnload 处。

I have a user control embedded in a web part. It has the following snippets of code:

protected TextBox searchTextBox;
protected Button goButton;
protected Button nextButton;
protected Repeater pushpins;
protected Label currentPageLabel;

protected void goButton_Click(object sender, EventArgs e)
{
 // Do stuff
}

<asp:TextBox ID="searchTextBox" runat="server" />
<asp:Button ID="goButton" runat="server" Text="Go" OnClick="goButton_Click" />
<asp:Repeater ID="pushpins" runat="server" EnableViewState="false">
 <ItemTemplate>Blah</ItemTemplate>
</asp:Repeater>
<asp:Label ID="currentPageLabel" runat="server" />
<asp:Button ID="nextButton" runat="server" Text=" >> " OnClick="nextButton_Click" />

(There is no OnLoad or CreateChildControls method.)

If I place a breakpoint on the first line of goButton_Click, I find:

  • searchTextBox: initialised
  • goButton: initialised
  • nextButton: NULL
  • pushpins: initialised
  • currentPageLabel: NULL

Why are some controls initialised and others not? How do I get around this if I'd like to update the Text property on currentPageLabel?

Update:

I've placed breakpoints all the way through the page life cycle and found that nextButton and currentPageLabel are never initialised. The breakpoints were placed at OnLoad, CreateChildControls, goButton_Click, OnPreRender, Render and OnUnload.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-08-26 17:16:05

您是否可能无意中在某处声明了名为 currentPageLabel 和/或 nextButton 的局部变量?编译器会很高兴地允许这样做...

这在从 ASP.NET 1.1 到 2.0 或更高版本的迁移中尤其常见,因为设计者定义这些控件的方式发生了变化。

Is it possible you've inadvertently got local variables named currentPageLabel and/or nextButton declared somewhere? The compiler will happily allow this...

This is especially common in a move from ASP.NET 1.1 to 2.0 or higher, since the way the designer defined these controls changed.

故事与诗 2024-08-26 17:16:05

问题是所有未初始化的控件都位于 Repeater 控件内的 HeaderTemplate 内。 (为了节省空间,我没有将所有代码粘贴到问题中,该死。)

我将它们移出,问题得到了解决。显然 HeaderTemplate 中的控件是由 Repeater 实例化的,不遵循正常的页面生命周期。

The problem was that all of the controls that weren't being initialised were inside a HeaderTemplate within a Repeater control. (To save space I didn't paste all of my code into the question, dang it.)

I moved them out and the problem is resolved. Obviously controls in the HeaderTemplate are instantiated by the Repeater and do not follow the normal page life cycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文