开发者

Access property of UserControl on MasterPage from content page

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 i开发者_JAVA技巧s 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?


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);

        }


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜