Jumat, 13 September 2013

How to bind RadioButtonList control with XmlDataSource in ASP.NET

Introduction

If you want to bind RadioButtonList control with the XmlDataSource then you first create a xml file with some child nodes. So take a Xml file into your projcet.

  • Right click on your project name--> Add-->Add new item
  • Select ".xml" file. Specify name of the file which you want.
<?xml version="1.0" encoding="utf-8" ?>
<Links>
  <link Text="Best Programming Blog" url="http://www.id-script.com" />
  <link Text="Best Gadget Blog" url="http://pc-gadgetworld.blogspot.com/" />
<link Text="Best ASP.NET Site" url="http://www.asp.net" />
<link Text="Best Search Engine" url="http://www.google.com" />
</Links>

Here is the simple structure of the file . <Links> tag is the root tag of other child tag and <Link> tag take some attribute such as Text and url . Now if you bind RadioButtonList control with XmlDataSource then you should specify DataText filed to text and DataVlaue Field is Url

Binding steps

Step-1: Drop RadioButtonList control to design window from toolbox.
Step-2: Choose DataSource using showsmart tag.
Step-3: Select <New Data Source> in open dialog box.
Step-4: Select Xml file in given DataSource.
Select Xml file in given DataSource
















Step-5: Browse xml file from project solution.


Browse xml file from project solution












Step-6: Select Text in DataText field and url in value field.


Select Text in DataText field and url in value field.

















Complete Source Code
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "You are selected " + RadioButtonList1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" DataSourceID="XmlDataSource1" DataTextField="Text" DataValueField="url">

        </asp:RadioButtonList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Output
How to bind RadioButtonList control with XmlDataSource in ASP.NET

Tidak ada komentar:

Posting Komentar