connectseekers.com

Monday, 11 August 2014

DropDownList Bind By Using XmlDataSource in Asp.Net

8/11/2014 - By Pranav Singh 0

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.


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.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top