Category       
  Server Script
  Client Script
  Data Base
  HTML - CSS
  Third Party
  Mobile Development
  Other
 
 Articles Detail
Posted Date : 10/2/2012     No Of Visit : 1062
Introduction : In this artile i will show you how to bind dropdownlist control to an xml file data using c# in asp.net
Other Related Tag
XML
 

 In this artile i will show you how to bind dropdownlist control to an xml file data using c# in asp.net. In this we will not use Sql data base as datasource but we will use XML.

So here we go.

First crerate a new web project. Create a new page and add dropdown control. After adding dropdown list you will get following code:.

<asp:DropDownList ID="ddllocation" runat="server">

</asp:DropDownList>

 

Now create a folder for .XML file in your project.

 


Now create a new file with name which you want. i have used DotnetXMLFile.xml.

Add the following XML code in this file

 

<?xml version="1.0" encoding="utf-8" ?>

<DotnetResources>

<Dotnet>

<ID>1</ID>

<Name>Asp.Net</Name>

</Dotnet>

<Dotnet>

<ID>2</ID>

<Name>C#.Net</Name>

<Dotnet>

<ID>3</ID>

<Name>VB.Net</Name>

</Dotnet>

<Dotnet>

<ID>4</ID>

<Name>Jquery</Name>

</Dotnet>

<Dotnet>

<ID>5</ID>

<Name>Ajax</Name>

</Dotnet>

</Dotnet>

</DotnetResources>


Now we will add the code for populating the dropdown list.


//page load event handler

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

//Function to populate dropdownlist from xml

PopulatedropdowndatafromXMLfile();

}

}

//Populates the dropdownlist from xml file

public void PopulatedropdowndatafromXMLfile()

{

string XMLfilePath = Server.MapPath("~/XMLResources/DotnetXMLFile.xml");
ds.ReadXml(XMLfilePath);

DataSet
ds = new DataSet();
ddllocation.DataSource = ds;
ddllocation.DataTextField =
"Name";
ddllocation.DataValueField =
"ID";
ddllocation.DataBind();

}


In above code string XMLfilePath = Server.MapPath("~/XMLResources/DotnetXMLFile.xml"); is the path of the file which we are binding to the dropdown.


Now press F5 and run the page. while running the page put a break point to check the dataset value. In you data set  you will get the XML file value.

 

now press F5 and come out od debuging mode. You will get following output.

 

 You May Also Like This
 blue arrow  How to Use jQuery For Creating Floating Menu with Content Scroll in MVC3
 blue arrow  Simple Signup Form in Asp.net Using C#
 blue arrow  Top 5 jQuery Voting Plugins For Your Website | Star Rating jQuery Plugin For Your Website
 blue arrow  Ajax Enabled Gridview Using AJAX Control Toolkit UpdatePanel in ASP.NET
 blue arrow  Code to Use jQuery toggleClass Effects | How to Resize Div Using jQuery
 blue arrow  jquery UI Datepicker to display multiple months | Jquery calender with multiple moth display in asp.net
 blue arrow  Validate Email Address Using Regular Expression In Asp.Net
 blue arrow  Show And Hide Divs At a Specific Time Interval Using jQuery
 blue arrow  How To Sent Mail More Than One Email Id One Click Event
 blue arrow  Ajax Validation For Duplicate Email Id In Asp.Net
 blue arrow  File Upload in MVC3 By Using Razor
 blue arrow  Code for Getting Gridview Row Values When Checkbox Selected in Asp.net Using C#
 blue arrow  Code To Access Controls Value of From One Page To Another Page In Asp.Net Using C#
 blue arrow  Code To Write to a Text File C# In Windows Application
 blue arrow  Code to Find Control Inside of Asp.Net Gridview in Rowdatabound event
 User Profile
Name : Vinay Singh
Company Name : -
Blog Name : -
About Me:
 User Comments       
Name : (max 50)
Your Comment : (max 800)
Posted By :Rahul Awasthi Posted Date :1/30/2013
 
Where you bind your dataset or where you give path of your xml file...
 
Posted By :Vinay Posted Date :1/30/2013
 
Hi rahul please check the code again . below mention in below code we have mention that the path of the xml file. string XMLfilePath = Server.MapPath("~/XMLResources/DotnetXMLFile.xml");
 
Posted By :Afnan ahmad Posted Date :4/4/2013
 
good tutorial thanks alots
 
Posted By :Vinay Posted Date :4/5/2013
 
Thanks for you comment..
 
Email Id
Password
Forgot Password   |   New Registration
;