Search code examples
c#asp.netlocalizationrepeaterkentico

Loop through a custom resource file in kentico 8


I've built a .aspxtemplate which contains a dropdown (no .asp control).

Template sample:

<select>
    <option selected="selected" value="">Bitte w&auml;hlen</option>
    <asp:Repeater ID="countryDropDown" runat="server">

        <HeaderTemplate>
            <option value="<%--key--%>">
        </HeaderTemplate>

        <ItemTemplate>
            <%--value--%>
        </ItemTemplate>

        <FooterTemplate>
            </option>
        </FooterTemplate>

    </asp:Repeater>
</select>

This dropdown should contain all countries. All countries are stored in Custom.resex which is placed in the CMSResoures folder which is Kentico specific.

Now i want to iterate through the file and pass a list of countries to the repeater. I'm not able to find a solution for this.

Of course it's easy to get a specific value through the key with the Kentico ResHelper.GetString("stringKey") function but not found a way to receive all entries using the Kentico library.


Solution

  • If you want to do this the super easy way, then just register the Country Selecter that's found in ~/CMSFormControls/CountrySelector.ascx like this:

    It has the same function as what you appear to be working on.

    However, for future reference, all of the countries are already stored in the CMS_Countries table in the DB. It would be much easier to populate a DataSet with all of the countries, then assign the DataSet as the repeater's datasource. Here is one of numerous ways to do this:

    //Set the connection string to the Kentico Database in the web.config to a variable called CMSConnectionString
    string CMSConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString;
    
    //Sets the text of the query we want to execute on the database to a variable called queryText
    string queryText = "SELECT * FROM dbo.CMS_Countries";
    
    //Creates a new instance of the SqlDataAdapter object and calls it "adapter".  
    //We pass in the text of the query we want to executre on the Kentico database, and the connetion string to the Kentico database.
    SqlDataAdapter adapter = new SqlDataAdapter(queryText, CMSConnectionString);
    
    //Creates a new instance of the DataSet object and calls it "countries".
    DataSet countries = new DataSet();
    
    //Fills the "countries" dataset with the data retrieved by our query on the Kentico database.
    adapter.Fill(countries);
    
    //Sets the datasource of the repeater to the dataset that we just created.
    repeater.DataSource = countries;
    
    //Binds the datasource to the repeater server control
    repeater.DataBind();
    

    Finally, if you absolutely have to use the CMS.resx file, then you should check out .how to populate a datatable with XML.