Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Bind DropDownList using XML File

Article:
Viewed:  974 
Posted On:  25/04/2013 10:14:34 
How to bind a drop down list control using xml file in ASP.NET. 

In this article I am going to show how to bind a drop down list control using XML file.

below is my XML file...

2.png

Image 1.

My program output will be:

1.png

Image 2.

My aspx code is :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>>Bind DropDownList Using XML File. | Dot Net CodeSG</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="20" cellspacing="20" width="40%" align="center" border="2">
            <tr>

               
<td align="center">
                    <asp:DropDownList ID="ddlCountry" runat="server" Width="170px">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 
My aspx.cs code is:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 

public partial class _Default : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(Server.MapPath("Country.xml"));
            DataView dataView = dataSet.Tables[0].DefaultView;
            dataView.Sort = "Id";
            ddlCountry.DataTextField = "Name";
            ddlCountry.DataValueField = "Name";
            ddlCountry.DataSource = dataView;
            ddlCountry.DataBind();
        }
    }
}

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us