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 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"))
开发者_StackOverflow社区 {
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 .
精彩评论