Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

layout - Sitecore: How to use sublayout parameters from codebehind?

How do I get the values from the "Parameters" field (second screenshot) in the code-behind of the sublayout?

I understand I can get/set parameters on a rendering (specifically sublayout) when it is added to the presentation details of an item, just as described here (Sitecore 6 - using parameters).

layout instance parameters

However I would like to use the parameters field from the layout definition item. In the codebehind of the file belonging to to layout definition I can cast the parent to a sublayout and that object also has a .Parameters property, however this doesn't contain the values I'd expect.

layout definition parameters

This is the Page_Load method in the control code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    var sublayout = ((Sublayout)this.Parent);
    string rawParameters = Attributes["sc_parameters"];
    NameValueCollection parameters =
      Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters); 
      //parameters contains values from "Additional parameters (first screenshot)

      //I do not know the sublayout item id or sublayout path, so how do I get
      //the values from the second screenshot?
}

Doublecheck still doesn't work, only additional parameters are shown:

Step 1 - Enter parameters

Step 2 - Add sublayout + parameters to presentation

Step 3 - Display parameters on sublayout

Step 4 - Validate result

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like this:

var sublayout = ((Sublayout)this.Parent);
NameValueCollection nvc = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);

Here's a blog post that make it easier to do with extension methods.

Here's a shared source module for Sitecore that wraps this up in a class as well. It was written by John West, CTO of Sitecore USA.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...