从内容页访问 MasterPage 上 UserControl 的属性

发布于 2024-11-29 02:28:54 字数 1052 浏览 0 评论 0原文

尝试从内容页获取母版页上显示的用户控件的属性值,但该值不正确。

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 技术交流群。

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

发布评论

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

评论(3

一花一树开 2024-12-06 02:28:54

您可以尝试重写页面的 OnLoadComplete,在页面生命周期的这个阶段,控件将加载数据。

protected override void OnLoadComplete(EventArgs e)
        {
            bool _value = Master.SessionSummaryControl.RejectedBatches;
            base.OnLoadComplete(e);

        }

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.

protected override void OnLoadComplete(EventArgs e)
        {
            bool _value = Master.SessionSummaryControl.RejectedBatches;
            base.OnLoadComplete(e);

        }
感性 2024-12-06 02:28:54

您什么时候调用 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.

仙女山的月亮 2024-12-06 02:28:54

我可能会在调试器中逐步执行此操作以查看发生了什么。

我最好的猜测是,在您尝试将结果加载到 _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

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