This article will show you how you can bind an asp.net
dropdownlist by xml file using c#.net. In this we will read the xml file into
dataset and then bind this dataset to the dropdownlist.
Some of my previous articles are as follows:
jQuery
DatePicker Calendar With Dropdown Month and Year in Asp.Net, Load
Partial View By Selecting Value in Dropdown Using C# in Asp.net MVC,
Bind
DropDownList Using Entity Framework in ASP.Net MVC Using C#, Display
DropdownList Item By Group in Asp.Net MVC Using C#.Net, DropdownList
Item With Custom Icon Image In Asp.net Using C#.Net, DropDownList
Bind By Using XmlDataSource in Asp.Net, Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net,
C#
Conversion of DateTime to 24 Hours Time in Asp.Net | Display 24 Hour Time In
DropDownList In Asp.net.
So for this article first we will create a new asp.net application, and we will create an xml file and add the below xml code as shown below format.
xml version="1.0" encoding="utf-8" ?>
<CountryModel>
<Country>
<id>1</id>
<name>India</name>
</Country>
<Country>
<id>2</id>
<name>Pakistan</name>
</Country>
<Country>
<id>3</id>
<name>Sri Lanka</name>
</Country>
<Country>
<id>4</id>
<name>Nepal</name>
</Country>
<Country>
<id>5</id>
<name>Bhutan</name>
</Country>
</CountryModel>
|
After this activity add the below code into your .aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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 Asp.net
DropdownList Control by XML File Data Using DataSet in C#.Net</title>
<link href="Styles/Site.css" rel="stylesheet"
type="text/css"
/>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" class="content">
<tr>
<td>
Country List
<br />
<asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
In above code I have added a dropdown list. In which I will
bind the xml data. now just check the .cs code to bind the record to dropdown
list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetData();
}
}
public
void GetData()
{
try
{
DataSet
dsResult = new DataSet();
dsResult.ReadXml(Server.MapPath("~/XML/XMLFile1.xml"));
if
(dsResult.Tables.Count != 0)
{
if
(dsResult.Tables[0].Rows.Count > 0)
{
DropDownList1.DataSource = dsResult.Tables[0];
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
}
catch
(Exception ex)
{
}
}
}
}
|
In above code I have used dataset ReadXml function to read
the xml file. After getting record in dataset I have passed as data source to
the dropdown list.
Now we have done. Run the application to check the output.
0 comments:
Please let me know your view