Selasa, 10 September 2013

How to bind DropDownList using XmlDataSource in ASP.NET

The XmlDataSource Control

The XmlDataSource control allows a data bound control to bind the data from a XML document. This control also supports XPath expressions that allows to return only certain nodes from the XML document.

Public Properties of XmlDataSource class

DataFile : Specifies the file name of an XML file that the data source binds to.

Advantage of XML File  

  • Its a Text file.
  • Use for carry data.
  • Build hierarchy of user defined tags. 
  • Best use in transformation.

The DropDownList Control

The DropDownList control displays the list of data as a drop-down list from which you can make a single selection. The DropDownList control exists within the System.Web.UI.WebControls namespace. You cannot select multiple items in this control because when you make a selection from the list, the list closes automatically.
The DropDownList control has no non-inherited methods or events. This class inherited the ListControl class.

Public Properties of DropDownList Class

SelectedIndex : Obtains or sets the index of the selected item in the control.

Application of DropDownList Control

  • In Registration page where you can select your country in given DropDownList.
  • In management project where you can select single option in given options.
Source : dreamhost.com

Lets take simple example

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "Your Selected Url is <br/>" + DropDownList1.SelectedValue;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="XmlDataSource1" DataTextField="Text" DataValueField="url" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
 
    </div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Output
How to bind DropdownList using XmlDataSource in ASP.NET

Tidak ada komentar:

Posting Komentar