This will show you how you can bind the XML file data to gridview as per Category
and SubCategory in asp.net using c#.net.
Some of my previous articles are as follows: CheckBoxList
Bind By Using XmlDataSource in Asp.Net, Confirmation
Message With Yes, No Button In Asp.Net Using ConfirmButtonExtender and
ModalPopupExtender, Watermark
In Asp.Net TextBox, Dynamically
Add TextBox Control and Detect TextBox Value in Asp.Net Using C#, Comment
System OR Form and Display In GridView Using C# In Asp.Net, Hide
Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net,
Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net.
So for this article first i have created a new asp.net application and add GridView, Button, Label, and also we will add label and CheckBoxList. Inside the gridview itemtemplate. Now bind the label control to databind. After adding all controls your page will look as shown below.
So for this article first i have created a new asp.net application and add GridView, Button, Label, and also we will add label and CheckBoxList. Inside the gridview itemtemplate. Now bind the label control to databind. After adding all controls your page will look as shown below.
Your Page code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication2.WebForm7"
%>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblMainCategory"
runat="server"
Text='<%# Eval("MainCategory") %>'
Font-Bold="false"
Style="font-weight:
700"></asp:Label>
<asp:Label ID="lblMainCatID"
runat="server"
Text='<%# Eval("Category_Id") %>' Visible="false"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1"
runat="server"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show selected Value" />
<br />
Text :
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
Id :
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
|
Now we will create a XML file to populate the data.
<?xml version="1.0" encoding="utf-8" ?>
<MainItem>
<Category>
<MainCategory>Color</MainCategory>
<Category Name="Green"
Id="1"></Category>
<Category Name="Blue"
Id="2"></Category>
<Category Name="Red" Id="3"></Category>
</Category>
<Category>
<MainCategory>Language</MainCategory>
<Category Name="English" Id="4"></Category>
<Category Name="Hindi"
Id="5"></Category>
</Category>
</MainItem>
|
After creating the XML file add the ML file into the
App_Data folder.
Now add the below mention code inside the .cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication2
{
public partial class WebForm7 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGrid();
}
}
/// <summary>
/// Bind the grid view as per the XML file data
/// </summary>
public
void BindGrid()
{
DataSet
objds = new DataSet();
objds.ReadXml(Server.MapPath("~\\App_Data\\XMLFile1.xml"));
if
(objds.Tables[0].Rows.Count > 0)
{
DataTable
objdtmaincat = new DataTable();
objdtmaincat =
objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory")
!= null).CopyToDataTable();
GridView1.DataSource = objdtmaincat;
GridView1.DataBind();
//Bind
the sub category (CheckBox List)
for
(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBoxList
chkSubItemList = (CheckBoxList)GridView1.Rows[i].FindControl("CheckBoxList1");
Label
lblMainCatID = (Label)GridView1.Rows[i].FindControl("lblMainCatID");
DataTable
objdtSubCat = new DataTable();
objdtSubCat =
objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory")
== null && m.Field<int>("Category_Id_0")
== Convert.ToInt32(lblMainCatID.Text)).CopyToDataTable();
chkSubItemList.DataSource
= objdtSubCat;
chkSubItemList.DataTextField = "Name";
chkSubItemList.DataValueField = "Id";
chkSubItemList.DataBind();
}
}
}
/// <summary>
/// Code to detect the selected value inside the grid view
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected
void Button1_Click(object
sender, EventArgs e)
{
string
selectedItemValue = "";
string
selectedItemText = "";
for
(int j = 0; j < GridView1.Rows.Count; j++)
{
CheckBoxList
CheckBoxList1 = (CheckBoxList)GridView1.Rows[j].FindControl("CheckBoxList1");
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 created a method to bind the grid view.
In this I have first read the XML file. And in this I have get xml data as
shown below format.
Now filter of data for main category and detection of the
control. After getting control I have filter the data as per the main category.
As per category I have get filter data shown below. I have bind the filter data
to the CheckBoxList.
After binding the detail output will look as shown below.
Now on button click selected check box value have been
detected and displayed.
it is such as a great article!!!
ReplyDeletecan we do the same using asp.net mvc?
Thanks for your valuable comment. Thanks for suggesting me a new article. i will do the same. keep visiting.
DeleteHi please check the same article in asp.net MVC
Deletehttp://www.aspdotnet-pools.com/2015/06/bind-xml-file-data-to-gridview-by_24.html
thank you for your good article!
Deletei have a question
is there any possibility to retrive the data from database into checkboxlist
such as
each row in the database contain question and answer
so for each row in the table required to create checkboxlist
i will appreciate if you could answer my question
Hi,
ReplyDeletei want to know how to bind databse(table) into checkbox as multiple choice question and determine the one or true answer
Hi you need to prepare the quiz content.
Delete