问题
I have two sublayouts: Grid-1-2 and Grid-2-1.
The two sublayouts are sharing a single ASCX file (not a good sitecore practice but i need it this way).
The problem is that in the ASCX codebehind, i want to see if the current selected grid is Grid-1-2 or Grid-2-1 ?!
I have tried using both Datasource and RenderingId techniques but to no effect.
EDIT
I was wondering if i can get the "Parameters" field from "Data" section of the sublayout. This would do the trick.
All suggestions are welcomed. Please Help !!
回答1:
Can you try with :
Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
Also other option is:
LayoutDefinition layoutDef = LayoutDefinition.Parse(Sitecore.Context.Item.Fields["__renderings"].Value);
string deviceId = Sitecore.Context.Device.ID.ToString();
DeviceDefinition curDeviceDef = layoutDef.GetDevice(deviceId);
RenderingDefinition renderingDef = curDeviceDef.GetRendering(Sitecore.Context.Database.Items["/sitecore/Layout/SubLayouts/MySublayout"].ID.ToString());
int controlIndex = curDeviceDef.GetIndex(renderingDef.UniqueId);
Control MyDotNetControl = Sitecore.Context.Page.Renderings[controlIndex].GetControl();
回答2:
We have the following methods on a base class that all our sublayout controls inherit from... you could just pass in a parameter on each sublayout to identify it and retrieve it using the GetParameter method.. e.g. name="Grid-1-2" etc
public string GetParameter(string key, string defaultValue = null)
{
Sublayout s = this.SitecoreSublayout;
if (s != null)
{
if (!String.IsNullOrWhiteSpace(s.Parameters))
{
NameValueCollection pars = HttpUtility.ParseQueryString(s.Parameters);
if (pars != null)
{
return pars[key];
}
}
}
return defaultValue;
}
protected Sublayout SitecoreSublayout
{
get
{
Sublayout parent = this.Parent as Sublayout;
return parent;
}
}
来源:https://stackoverflow.com/questions/34060128/sitecore-8-get-sublayout-item-when-ascx-file-is-shared