Introduction: In previous articles i explained Bind state categories and cities sub categories in single dropdownlist and Create,Read,Sort Xml file and Bind to RadioButtonList and Create,Read,Sort Xml file and Bind to ListBox and Create,Read,Sort Xml file and Bind to CheckBoxList and How to read from XML file and bind to gridview in asp.net and Fill CheckBoxList based on DropDownList selection and Fill dropdownlist with days, month and year and Example to Validate DropDownList using jQuery in asp.net.
Description: In this article I am going to explain with example How to Read, Sort and Bind/Load/Fill DropDownList control with data from XML file in sorted order in asp.net. Here DropDownList is binded with data in sorted order of Country Name. As we all know xml file can be used as a Data Source and it’s a free and fastest way to travel data. So in this example we are using Xml file as a data source. If you are interested in binding DropDownList with Sql server database then you can read my article “How to fill DropDownList from Sql server database in asp.net”.
Implementation: Create a new website and add XML file. To add XML file follow the steps:
Description: In this article I am going to explain with example How to Read, Sort and Bind/Load/Fill DropDownList control with data from XML file in sorted order in asp.net. Here DropDownList is binded with data in sorted order of Country Name. As we all know xml file can be used as a Data Source and it’s a free and fastest way to travel data. So in this example we are using Xml file as a data source. If you are interested in binding DropDownList with Sql server database then you can read my article “How to fill DropDownList from Sql server database in asp.net”.
Implementation: Create a new website and add XML file. To add XML file follow the steps:
Go to Website Menu -> Add New
Item -> Select XML File and give name “Countries.xml”. Then paste the
following in the Countries.xml file
<Countries>
<Country>
<Id>1</Id>
<Name>India</Name>
</Country>
<Country>
<Id>2</Id>
<Name>USA</Name>
<Country>
<Id>3</Id>
<Name>Japan</Name>
</Country>
<Country>
<Id>4</Id>
<Name>Australia</Name>
</Country>
<Country>
<Id>5</Id>
<Name>England</Name>
</Country>
<Country>
<Id>6</Id>
<Name>UAE</Name>
</Country>
<Country>
<Id>7</Id>
<Name>China</Name>
</Country>
</Countries>
- Now in the design page (.aspx) Place a DropDownList and a Label control from the visual studio toolbar and set their properties as:
<fieldset style="width:300px;">
<legend>Bind DropDownList from Xml File in asp.net</legend>
Select Country:
<asp:DropDownList ID="ddlCountries"
runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlCountries_SelectedIndexChanged" Width="183px">
</asp:DropDownList>
<asp:Label ID="lblStatus"
runat="server"
ForeColor="#009933"></asp:Label>
</fieldset>
C#.Net Code to Read,Sort and Bind DropDownList with XML file data in asp.net
- In the code behind file (.aspx.cs) add namespace :
using System.Data;
then write the code as:
protected void Page_Load(object
sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDropDownListFromXML();
}
}
public void
BindDropDownListFromXML()
{
DataSet ds = new DataSet();
try
{
//Reading the data from the XML file
ds.ReadXml(MapPath("~/Countries.xml"));
DataView dv =
ds.Tables[0].DefaultView;
//Sort the DataView by "Name"
dv.Sort = "Name";
//Setting DataText field and DataValue field
of DropDownList
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "ID";
//Binding the DropDownList with the DataView
ddlCountries.DataSource = dv;
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("--
Select --", "0"));
}
catch (Exception
ex)
{
Response.Write(ex.Message.ToString());
}
}
protected void
ddlCountries_SelectedIndexChanged(object
sender, EventArgs e)
{
lblStatus.Text = "Selected Country :
" + ddlCountries.SelectedItem.Text + "
& " + "Selected value : "
+ ddlCountries.SelectedValue;
}
VB.Net Code to Read,Sort and Bind DropDownList with XML file data in asp.net
- In the design page (.aspx) Place a DropDownList and a Label control from the visual studio toolbar and set their properties as:
<fieldset style="width:300px;">
<legend>Bind DropDownList from Xml File in asp.net</legend>
Select Country :
<asp:DropDownList ID="ddlCountries"
runat="server"
AutoPostBack="True"
Width="183px">
</asp:DropDownList>
<asp:Label ID="lblStatus"
runat="server"
ForeColor="#009933"></asp:Label>
</fieldset>
- In the code behind file (.aspx.vb) add namespace :
Imports System.Data
Then write the code as:
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load
If Not
Page.IsPostBack Then
BindDropDownListFromXML()
End If
End Sub
Public Sub
BindDropDownListFromXML()
Dim ds As New DataSet()
Try
'Reading the data from the XML file
ds.ReadXml(MapPath("~/Countries.xml"))
Dim dv As
DataView = ds.Tables(0).DefaultView
'Sort the DataView by "Name"
dv.Sort = "Name"
'Setting DataText field and DataValue field of
DropDownList
ddlCountries.DataTextField = "Name"
ddlCountries.DataValueField = "ID"
'Binding the DropDownList with the DataView
ddlCountries.DataSource = dv
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("--
Select --", "0"))
Catch ex As Exception
Response.Write(ex.Message.ToString())
End Try
End Sub
Protected Sub
ddlCountries_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles
ddlCountries.SelectedIndexChanged
lblStatus.Text = "Selected Country :
" & ddlCountries.SelectedItem.Text & " & " & "Selected
Value : " & ddlCountries.SelectedValue
End Sub
Now over to you:
"If you like my work; you can appreciate by leaving your comments,
hitting Facebook like button, following on Google+, Twitter, Linked in and
Pinterest, stumbling my posts on stumble upon and subscribing for receiving
free updates directly to your inbox . Stay tuned and stay connected for more
technical updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..