Monday, August 8, 2016

How to fill Dropdownlist from an xml in ASP.NET

In this example I will explain how to bind a Dropdownlist control from an xml file.

Suppose a webpage contains Dropdownlist and it has to be filled with data from an xml,
we can use XmlDataSource for that and specify DataSourceID of dropdownlist to the Id of XmlDataSource.

Consider we have an xml file as follows which is saved in App_Data folder of the website:
App_Data/status.xml

<?xml version="1.0" encoding="utf-8"?>
<LookUp>
    <ListValue List_Id="" List_Desc=""/>
    <ListValue List_Id="1" List_Desc="Active"/>
    <ListValue List_Id="2" List_Desc="Disabled"/>
    <ListValue List_Id="3" List_Desc="Deleted"/>

</LookUp>


HTML Code (.aspx) :


<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"
        DataTextField="List_Desc" DataValueField="List_Id"
         DataSourceID="XmlDataSource1">
        </asp:DropDownList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"
        DataFile="~/App_Data/status.xml"></asp:XmlDataSource>
    </div>
    </form>
</body>


Output :




No comments:

Post a Comment