ASP.NET 用户控件:Page_Load 在设置属性之前触发
这让我发疯。
我有一个非常简单的用户控件:
public int? ImageId {set; get;}
protected void Page_Load(object sender, EventArgs e)
{
... do something with ImageId...
}
然后我将此控件放在 UpdatePanel 内的 ListView 页面上:
<asp:ListView ID="ListViewImages" runat="server" DataSourceID="src">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<My:MyControl ImageId='<%# Eval("Id") %>' ID="cipPreview" runat="server" />
</ItemTemplate>
</asp:ListView>
问题是 Page_Load 在 ASP.NET 设置 ImageId 之前触发。在调试器的帮助下,我发现由于某种原因,MyControl 中的 ImageId 已设置,但只有在 Page_Load 完成处理后才会发生。怎么了?
This is driving me crazy.
I have a very simple user control:
public int? ImageId {set; get;}
protected void Page_Load(object sender, EventArgs e)
{
... do something with ImageId...
}
And then I put this control on the page with ListView within UpdatePanel:
<asp:ListView ID="ListViewImages" runat="server" DataSourceID="src">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<My:MyControl ImageId='<%# Eval("Id") %>' ID="cipPreview" runat="server" />
</ItemTemplate>
</asp:ListView>
The problem is Page_Load fires BEFORE ASP.NET sets ImageId. With debugger's help I found out that for some reason ImageId in MyControl IS SET, but it happens only after Page_Load has finished processing. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为 ListView 上的数据绑定是在 Page_Load 触发之后发生的,因此此时您的属性尚未设置。您可以将代码移至 PreRender 事件,因为它是在数据绑定完成后调用的。
更多信息参见 MSDN:
- 在此事件发生之前:
It's probably because data binding on the ListView happens AFTER Page_Load fires, so therefore your property isn't set at that point. You could move your code to PreRender event since it is called after data binding is completed.
More info according to MSDN:
PreRender -- Before this event occurs: