Bind XML Data to DropDownList Control Using in Asp.net C#

Bind XML Data to DropDownList Control

Bind Extensible Markup Language (XML) Xml List Data Bind To Dropdownlist Control Using in Asp.Net C#.

                                   DEMO



                         Download

                       HTML Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
       
        Course
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>


                           C# Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class Xml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("XMLFile.xml"));

            DropDownList1.DataTextField = "coursename";

            DropDownList1.DataValueField = "courseid";

            DropDownList1.DataSource = ds;
            DropDownList1.DataBind();

        }
    }

}


First - Add NewWeb Form - Select Dropdownlist from

 Toolbox





Next - Add - New Item - XML file






Select - Xml File - Add 






Add Course Details To Xml File






Next - Add Namespaces -  Bind DataTextfield & DataValueField








0 comments:

Post a Comment