Friday, 15 August 2014

CheckBoxList Bind By Using XmlDataSource in Asp.Net

8/15/2014 - By Pranav Singh 5

This article will show you how you can bind a CheckBoxList 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 asp.net application. After creating a new application first we will create a new XML file and put this XML file into App_Data folder.



Here is the XML file data.

xml version="1.0" encoding="utf-8" ?>
<Country>
  <State Name="Uttar Pradesh" Id="1"></State>
  <State Name="Maharastra" Id="2"></State>
  <State Name="Delhi" Id="3"></State>
  <State Name="Bihar" Id="4"></State>
  <State Name="Asam" Id="5"></State>
</Country>

Now we will add an XML data source and a CheckBoxList 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>CheckBoxList Bind By Using XmlDataSource in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="XmlDataSource1"
            DataTextField="Name" DataValueField="Id">
        </asp:CheckBoxList>
         <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>

Now we will add code to get the selected value of the CheckBoxList on button click.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class XMLDropdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string selectedItemValue = "";
            string selectedItemText = "";
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected == true)
                {
                    selectedItemValue = selectedItemValue + CheckBoxList1.Items[i].Value + ",";
                    selectedItemText = selectedItemText + CheckBoxList1.Items[i].Text + ",";
                }
                Label1.Text = selectedItemText;
                Label2.Text = selectedItemValue;
            }
        }
    }
}

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 some item and click on submit button.



 DOWNLOAD

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

5 comments:

  1. Hi
    can we do the some if we want to bind xml to many different checkboxes to be appear as follows if we have just one xml file

    1. color
    ( ) green ( ) red ( ) white

    2 language

    ( ) English ( ) Turkish ( ) Arabic

    3 nationality
    ( ) xxxx ( ) yyyyyy ( )uuuuuu

    4 education
    ( )kkkkkk ( ) llllllllll ( ) oooooooo
    .
    .
    .
    and so on
    please the answer very important for me i will appreciate if answer me soon

    ReplyDelete
    Replies
    1. Hi Renas Wria
      Yes it is possible. we can do this.
      Thanks for your query. Give me some tine 1 or 2 days .i will look into this issue and let you know the exact solution.

      Delete
    2. really I appreciate that
      thank you

      Delete
    3. If possible please provide your XML structure

      Delete
    4. Hi Renas,

      As per request i have written an article. it will show output as per your given detail. have a look of the the article link.

      http://www.aspdotnet-pools.com/2015/06/bind-xml-file-data-to-gridview-by.html

      Delete

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

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


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top