0

I have two sublayouts: Grid-1-2 and Grid-2-1.

enter image description here

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.

enter image description here

All suggestions are welcomed. Please Help !!

  • 2
    What exactly are you trying to do? I presume you are trying to dynamically change the column widths? You should look into [using Rendering Parameters for this type](https://www.captechconsulting.com/blogs/sitecore-rendering-parameters-part-1--how-and-why-to-use-rendering-parameters) of thing... – jammykam Dec 03 '15 at 10:14
  • @Jammy Yes. I am doing exactly what you said. I looked through the blog but have a blocker. What about the items that were created before the template transformation. I am not sure the items would keep the data after changing the template. However, i can use The "Parameters" field from "Data" section of sublayout. Any idea on how to access the field ? – Prathamesh dhanawade Dec 03 '15 at 11:04
  • Possible duplicate of [Sitecore: How to use sublayout parameters from codebehind?](http://stackoverflow.com/questions/5949214/sitecore-how-to-use-sublayout-parameters-from-codebehind) – Ben Golden Dec 03 '15 at 15:11
  • @Ben My case is a little different though. I have two sublayout items sharing same ascx file. – Prathamesh dhanawade Dec 03 '15 at 15:23
  • Since the Sublayout is not a template, the items already created using this control would not lose the data, you would still have to switch any existing usage of the Sublayout you wish to remove with the other one. The easiest way would be to delete the existing sublayout and then link to another item when prompted: http://imgur.com/J8CMDfY – jammykam Dec 03 '15 at 17:16
  • @jammy I didnt get you. I do not to switch my sublayouts. I just want to know (in code behind) which sublayout item is being invoked from among the two. – Prathamesh dhanawade Dec 04 '15 at 04:16

2 Answers2

0

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();
Vlad Iobagiu
  • 4,118
  • 3
  • 13
  • 22
  • I tried the datasource and renderingId ways but didnt work. In second technique , why do i need to hardcode my sublayout items..?? My question is with 2 sublayouts sharing the ascx file and checking which sublayout is currently using the ascx file !! – Prathamesh dhanawade Dec 03 '15 at 10:10
0

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;
        }
    }
davey_lad
  • 41
  • 2