从内容页访问 MasterPage 上 UserControl 的属性
尝试从内容页获取母版页上显示的用户控件的属性值,但该值不正确。
MasterPage APSX:
<div style="float: left">
<uc:SessionSummary ID="SessionSummary1" runat="Server" />
</div>
MasterPage CS:
public SessionSummary SessionSummaryControl
{
get { return (SessionSummary)this.FindControl("SessionSummary1"); }
}
UserControl:
public bool RejectedBatches
{
get { return lblRejectedBatches.Text.ToUpper().Trim() == "YES" ; }
}
ContentPage ASPX:
<%@ MasterType VirtualPath="MAASDefault.master" %>
<%@ Reference VirtualPath="MAASDefault.master" %>
ContentPage CS:
bool _value = Master.SessionSummaryControl.RejectedBatches;
_value 始终为 false,即使控件上显示的文本为“是”也
没有看到我缺少的内容。任何帮助将不胜感激。
编辑- 好吧,这是我的德利马。我检查了几个页面事件,找到了一个能给我价值的页面事件。但是,由于该属性位于母版页上,并且母版页是内容页的子控件,因此该值仅在内容页的 Page_Load 事件之后才可用。我需要根据来自母版的该值在内容页面中呈现控件。想法?
Trying to get the value of a property on a usercontrol displayed on a masterpage from a content page but the value is not correct.
MasterPage APSX:
<div style="float: left">
<uc:SessionSummary ID="SessionSummary1" runat="Server" />
</div>
MasterPage CS:
public SessionSummary SessionSummaryControl
{
get { return (SessionSummary)this.FindControl("SessionSummary1"); }
}
UserControl:
public bool RejectedBatches
{
get { return lblRejectedBatches.Text.ToUpper().Trim() == "YES" ; }
}
ContentPage ASPX:
<%@ MasterType VirtualPath="MAASDefault.master" %>
<%@ Reference VirtualPath="MAASDefault.master" %>
ContentPage CS:
bool _value = Master.SessionSummaryControl.RejectedBatches;
_value is always false even though the text displayed on the control is 'Yes'
Not seeing what I'm missing. Any help would be appreciated.
edit-
Ok here is my delima. I have checked several page events and found one that will give me the value. But, since the property is on the masterpage and the masterpage is a child control of the content page the value is only available after the Page_Load event of the content page. I need to render controls in the content page based on this value from the master. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试重写页面的 OnLoadComplete,在页面生命周期的这个阶段,控件将加载数据。
You can try overriding the OnLoadComplete of the page, at this stage of the page life cycle the control will be loaded with the data.
您什么时候调用 Master.SessionSummaryControl.RejectedBatches ?如果是在用户控件受数据限制或初始化之前,那么您将会遇到此问题。
请参阅页面生命周期以确保您足够晚地访问用户控件。
When are you calling
Master.SessionSummaryControl.RejectedBatches
? If it's before the user control is data bounded or initialized then you will have this problem.Please refer to page life cycle to make sure that you are accessing user control late enough.
我可能会在调试器中逐步执行此操作以查看发生了什么。
我最好的猜测是,在您尝试将结果加载到 _value 后, lblRejectedBatches 的文本会被设置
I would probably step through this in the debugger to see what's going on.
My best guess is that the text for lblRejectedBatches is getting set sometime after you're trying to load the result into _value