Redirecting to default.aspx when another culture is clicked

   —   
For example you want to load default.aspx page of requested culture, instead of current document’ s requested culture. E.g. when you are on services.aspx and click different culture, you want to be redirected to default.aspx instead of staying on services.aspx in new culture version.
Solution is to modify appropriate control in:
<project folder>/CMSWebParts/General/languageselectionwithflags.ascx.cs

By changing following piece of code (this example works only for first level documents, for all level see example below):

if (this.ShowCultureNames)
{
ltlHyperlinks.Text += "<a href=\"" + UrlHelper.AddUrlParameter(queryString, "lang", dr["CultureCode"].ToString()) + "\">";
ltlHyperlinks.Text += dr["CultureShortName"].ToString();

// Set surrounding div css class
selectionClass = "languageSelectionWithCultures";
}
else
{
ltlHyperlinks.Text += "<a href=\"" + UrlHelper.AddUrlParameter(queryString, "lang", dr["CultureCode"].ToString()) + "\">" + "<img src=\"" + imgFlagIcon + "\" alt=\"" + dr["CultureShortName"].ToString() + "\" />";

// Set surrounding div css class
selectionClass = "languageSelection";
}

into this new:

if (this.ShowCultureNames)
{
ltlHyperlinks.Text += "<a href=\"" + UrlHelper.AddParameterToUrl("default.aspx", "lang", dr["CultureCode"].ToString()) + "\">";
ltlHyperlinks.Text += dr["CultureShortName"].ToString();

// Set surrounding div css class
selectionClass = "languageSelectionWithCultures";
}
else
{
ltlHyperlinks.Text += "<a href=\"" + UrlHelper.AddParameterToUrl("default.aspx", "lang", dr["CultureCode"].ToString()) + "\">" + "<img src=\"" + imgFlagIcon + "\" alt=\"" + dr["CultureShortName"].ToString() + "\" />";

// Set surrounding div css class
selectionClass = "languageSelection";
}


Please note what was changed:
1. method AddUrlParameter into AddParameterToUrl.
2. Attribute queryString directly into "default.aspx" string.

Code before else statement runs, if you have "Show culture names" property enabled. If you want to click the flag(s), then code after else statement runs. I have change it for both cases.

Example for documents on all levels:

Under “foreach (DataRow dr in ds.Tables[0].Rows)” in SetupControl() method please add:

string YourDomainName = "http://www.mydomain.com/"; //Please change to requested domain name. Must be ended by slash "/"!
string YourHomePageAspxFileName = "default.aspx"; // This is default value, please do not change
/////////

Then please change this piece of code:

if (this.ShowCultureNames)
{
    // original code
}
else
{
    // original code
}

Like folloving:

if (this.ShowCultureNames)
{
    ltlHyperlinks.Text += "<a href=\"" + YourDomainName + YourHomePageAspxFileName + "?lang=" + dr["CultureCode"].ToString() + "\">";
    ltlHyperlinks.Text += dr["CultureShortName"].ToString();

   // Set surrounding div css class
   selectionClass = "languageSelectionWithCultures";
}
else
{
    ltlHyperlinks.Text += "<a href=\"" + YourDomainName + YourHomePageAspxFileName + "?lang=" + dr["CultureCode"].ToString() + "\">";

   // Set surrounding div css class
   selectionClass = "languageSelection";
}




See also:


Applies to: Kentico CMS 3.x
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.