This article will show you how you can bind a dropdown list
by using XML data source and how to retrieve the bind text and value on button
click event using c#.net in asp.net.
Some of my previous articles are as follows:
Disable
jQuery UI DatePicker Calendar On Button Click In Asp.Net, Bind
DropDownList Using Entity Framework in ASP.Net MVC Using C#, C#
Conversion of DateTime to 24 Hours Time in Asp.Net | Display 24 Hour Time In
DropDownList In Asp.net, Load
Partial View By Selecting Value in Dropdown Using C# in Asp.net MVC,
jQuery
DatePicker Calendar With Dropdown Month and Year in Asp.Net, How
to Bind xml to DataGridView Using C#.Net In Windows Application, Read
XML File into DataSet and Populate in ComboBox Using C# in Windows Application,
Gridview
Export to XML in Asp.Net Using C#.Net.
So for this article first we will create a new XML file and
add this file into our App_Data folder.
Now open add some xml tag in this file.
xml version="1.0" encoding="utf-8" ?>
<Country>
<State Name="Uttar Pradesh" Id="1"></State>
<State Name="Maharashtra" Id="2"></State>
<State Name="Delhi" Id="3"></State>
<State Name="Bihar" Id="4"></State>
<State Name="Assam" Id="5"></State>
</Country>
|
Now we will add an XML data source and a dropdown list into
our page and provide the path if the XML file to the XML datasource.
Now here is the complete code of your .aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XMLDropdown.aspx.cs" Inherits="ProjectDemo_Asp.et.XMLDropdown"
%>
<!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>DropDownList
Bind By Using XmlDataSource in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="226px" DataSourceID="XmlDataSource1"
DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Submit"
/>
</div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/XML/Country.xml">
</asp:XmlDataSource>
<p>
Text:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
<p>
Id:
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</p>
</form>
</body>
</html>
|
In above code just check the dropdown and the XmlDataSource.
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="226px" DataSourceID="XmlDataSource1"
DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Submit"
/>
</div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/XML/Country.xml">
</asp:XmlDataSource>
|
Now we will add code to get the selected value of the dropdown on button click.
protected void
Button1_Click(object sender, EventArgs e)
{
Label1.Text =
DropDownList1.SelectedItem.Text;
Label2.Text =
DropDownList1.SelectedItem.Value;
}
|
In above code I have assigned the I have assigned DataSourceID
to dropdown and the Text field and the data value field to the dropdown. Now we
have done run the page.
Now select the state and click on button to submit the form.
0 comments:
Please let me know your view