0

I want to create language selector in the site header. So for that i am trying to pick all language(/sitecore/system/Languages) from Sitecore dynamically using repeater. I am able to get language values from Sitecore but in the page i am not getting language dropdown.

My view Code :

    <div class="ddlCountry"> Select Language:
    <asp:Repeater ID="rpLang" ItemType="Sitecore.Data.Items.Item" runat="server">
        <HeaderTemplate>
            <DropDownList> 
        </HeaderTemplate>
        <ItemTemplate>
            <listitem text=<%#: Item.Name %> runat="server"/>
             </ItemTemplate>
        <FooterTemplate>
            </DropDownList>
        </FooterTemplate>
    </asp:Repeater>
</div>

My Back-end Code:

public partial class CountryRegionSublayout : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Item lang = Sitecore.Context.Database.GetItem("/sitecore/system/Languages");

            Item[] children = lang.Children.ToArray();
            rpLang.DataSource = children;
            rpLang.DataBind();


    }
}
  • Instead of using the listitem, can you please use the asp:DropDownList – Hishaam Namooya Sep 25 at 6:56
1

Ideally to display , languages in a dropdown , you should use a droplist.

<asp:DropDownList runat="server" ID="languagesDropDown" EnableViewState="False" />

To display the items in Key/Value pair in the dropdown, Modify the code below accordingly

    protected override void OnInit(EventArgs e)
    {
        foreach (var definition in LanguageDefinitions.Definitions)
        {
            //var cultureInfo = Language.CreateCultureInfo(definition.Name);
            var listItem = new ListItem(Language.GetDisplayName(cultureInfo), definition.Name);
            this.languagesDropDown.Items.Add(listItem);
        }

        //base.OnInit(e);
    }

Please take reference from the InstallLanguage.aspx available in /sitecore/admin folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.