English 中文(简体)
Passing Parameters to a Sitecore Sublayout
原标题:
  • 时间:2010-01-07 14:42:29
  •  标签:
  • sitecore

I ve done this before with a web control, but I can t seem to get it to work with a sublayout. On the Presentation Details for a particular item I m assigning my Sublayout and then in the additional parameters section specifying the parameter. Here s the code that s in the code-behind for my sublayout. When I run the debugger, RenderPageTitle is just null.

public partial class PageContent : System.Web.UI.UserControl
{
    public String RenderPageTitle { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (RenderPageTitle.ToLower().Equals("false"))
        {
            TitleFieldRenderer.Visible = false;
        }
    }
}
最佳回答

please refer to this blog post.

For sitecore6, in the .cs file:

string rawParameters = this.Parameters;
NameValueCollection parameters = Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters);

or in .ascx file:

string rawParameters = Attributes["sc_parameters"];
NameValueCollection parameters = Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters);
问题回答

There may be some better way to do this. It s hard to say.

The parameters to a sublayout are URL encoded (HttpUtility.UrlEncode or similar) and joined together like a querystring, and then placed in the "sc_parameters" attribute of the control.

So, like chiesa said, in a web user control (this is what that blog meant by .ascx file) you can do this:

string rawParameters = Attributes["sc_parameters"];
NameValueCollection parameters = 
  Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters);

And then you have the parameters as a dictionary of strings. However, these are still encoded, so if they contain anything other than letters and numbers you will probably want to use something like HttpUtility.UrlDecode to fix them.

string color_scheme = HttpUtility.UrlDecode(parameters["ColorScheme"]);
int ash_diffuser_id = // Could have a + sign prepended or something.
  HttpUtility.UrlDecode(Int32.Parse(parameters["AshDiffuserID"]));

You can have sublayout s parameter value by declaring _Param variable with NameValueCollection data type and refer tem to get the specific parameter value by passing the key value . This way this common function can reside into helper file and can be reused .

Here is the code snippet.

// All known parameters passed to the sublayout.
static NameValueCollection _params = null;

/// <summary>
/// Return the value of a specific parameter.
/// </summary>
/// <param name="key">Parameter name.</param>
/// <returns>Value of specified parameter.</returns>
public static string GetParam(string key)
{
    key.Trim().ToLower();
    string result = _params[key.Trim().ToLower()];

    if (String.IsNullOrEmpty(result))
     {
          result = String.Empty;
     }

    return (System.Web.HttpUtility.UrlDecode(result));
}

You can get the value of the parameter that you passed in the sublayout simply by passing the key name of the parameter as an argument of this function .

Hope this helps .





相关问题
How do I hook the Save event in Sitecore Page Editor?

I m creating a custom edit control for my content authors to use in the Page Editor. Of course this means I want my control to save data to an Item when the user clicks on the Save button. How is ...

Passing Parameters to a Sitecore Sublayout

I ve done this before with a web control, but I can t seem to get it to work with a sublayout. On the Presentation Details for a particular item I m assigning my Sublayout and then in the additional ...

Lucene.Net Search List

I m using Sitecore and have a multilist field I d like to use Lucene to search on. The problem I have is that the field is a pipe-delimited list of actual values and there could be between 0 and an ...

Test for Descendant Items

I am trying to create a simple inventory of items in a Sitecore website using an xslt. The problem is that I can t find a way to test whether an item has descendant items beneath it. It s easy to ...

Sitecore set/change default language from en

Can anyone recommend the best way to change the default language in Sitecore 6.1. On a fresh install, the typical language for all items is en English, witha nice little USA flag and all. However, ...

热门标签