Loading Items from XML
You can populate the ComboBox with Items from an XML file out of the Box.
Add a ComboBox component to the Page.
<telerik:RadComboBox ID="RadComboBox1" runat="server"></telerik:RadComboBox>
Load the XML file using any of the following approaches:
Loading from an XML file
Create an XML file with the following structure:
- Root element:
<Items></Items>
- Child elements:
<Item />
All ComboBox Item Properties can be mapped on the respective XML nodes as attributes. In addition, you can use any Custom Attributes that are not available as properties.
~/App_Data/Continents.xml
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<Item Text="Africa" Value="1" Enabled="True" />
<Item Text="Australia" Value="2" Selected="False" />
<Item Text="Asia" Value="3" CustomAttribute="CustomData" />
<Item Text="Europe" Value="4" />
<Item Text="North America" Value="5" />
<Item Text="South America" Value="6" />
</Items>
Loading from XML File using the
LoadContentFile
method
protected void Page_Init(object sender, EventArgs e)
{
RadComboBox1.LoadContentFile("~/App_Data/Continents.xml");
}
Loading from an XML string
Create a string with the following XML structure (or fetch it from a database, for example):
- Root element:
<Items></Items>
- Child elements:
<Item />
All ComboBox Item Properties can be mapped on the respective XML nodes as attributes. In addition, you can use any Custom Attributes that are not available as properties.
Loading from XML String using the
LoadXml
method
protected void Page_Init(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.Append("<Items>");
sb.Append(" <Item Text=\"Africa\" Value=\"1\" Enabled=\"True\" />");
sb.Append(" <Item Text=\"Australia\" Value=\"2\" Selected=\"False\" />");
sb.Append(" <Item Text=\"Asia\" Value=\"3\" CustomAttribute=\"CustomData\" />");
sb.Append(" <Item Text=\"Europe\" Value=\"4\" />");
sb.Append(" <Item Text=\"North America\" Value=\"5\" />");
sb.Append(" <Item Text=\"South America\" Value=\"6\" />");
sb.Append("</Items>");
string xmlString = sb.ToString();
RadComboBox1.LoadXml(xmlString);
}