为什么不是所有控件都在 ASP.NET 页面生命周期的事件处理点初始化?
我在 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
:初始化nextButton
:NULL图钉
: 已初始化currentPageLabel
: NULL
为什么有些控件已初始化,而另一些则没有?如果我想更新 currentPageLabel
上的 Text
属性,如何解决此问题?
更新:
我在页面生命周期的整个过程中都放置了断点,发现 nextButton
和 currentPageLabel
从不 已初始化。断点放置在 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
: initialisedgoButton
: initialisednextButton
: NULLpushpins
: initialisedcurrentPageLabel
: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否可能无意中在某处声明了名为 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.
问题是所有未初始化的控件都位于 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.