Reputation: 109
I have a sublayout that has an asp.net wizard. For each wizard step I want to be able to call a asp.net user control to display the content.
I created a template that has some fields and I want to be able to access these fields in my user controls.
In my user control I tried getting my sublayout by doing the following:
Sublayout thisSublayout = (Parent as Sublayout);
But this returns a null.
Any ideas what I'm doing wrong?
Thanks in advance.
Below is the code i'm using:
MySublayout.ascx
All i'm doing here is dropping my user control on the page. The template that I associate with this sublayout has a "My Template Field".
<uctrl:MyNestedUC ID="ucMyUserControl" runat="server" >
</uctrl:MyNestedUC>
MyNestedUC.ascx
This page just has a scText attribute on it:
<sc:Text runat="server" ID="scMyTemplateField" Field="My Template Field" />
MyNestedUC.ascx.cs
In my pageload method this is all i'm doing:
scMyTemplateField.Item = Sitecore.Context.Item;
The scMyTemplateField is null so trying to access scMyTemplateField.Item returns a null reference error.
The directives at the top of each page look like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="My Sublayout.ascx.cs" Inherits="Sublayouts.content.My_Sublayout" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyNestedUC.ascx.cs" Inherits="Controls.MyNestedUC" %>
Upvotes: 0
Views: 1878
Reputation: 2017
You can statically bind sublayout like given below -
sc:Sublayout ID="slFooterUtilityNav" Path="~/Presentation/Sublayouts/Controls/Shared/FooterUtilityNav.ascx" runat="server" />
I think, it will work for you.
Upvotes: 0
Reputation: 7994
As @TwentyGotoTen mentions, you probably are missing a level for accessing the Sublayout.
That being said, if the fields are on the item, you won't need to access the Sublayout. You can directly query the Sitecore.Context.Item from your user control.
If the fields are on a Sublayout parameter template, or are on a component datasource attached to your sublayout, you will have to crawl up the parent tree to find the parent that is a Sublayout.
Alternatively, and this is my preferred approach, when instantiating user controls on a Sublayout I pass any necessary data to the user control on public properties. This allows for the user control to be used in any situation, so long as it is initialized correctly.
For example, you might add a public property on the user control named 'DataSource' with type 'Item', and then have your sublayout specify the property value as it is initialized.
Upvotes: 0
Reputation: 4456
Maybe I've misunderstood what you're saying, but if you're referencing the sublayout from a user control that is inside another user control, then it would probably be the "grand parent" that you need to cast:
Sublayout thisSublayout = (Parent.Parent as Sublayout);
Upvotes: 0