Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
here, i wanted to bind data in one datalist when we choose checkbox.

assume. i have three category called football, volleyball, bat.

here when i tick in checkbox1, it should show football in the datalist at right-hand side.

if i tick checkbox1, and checkbox2 both, It should show football, and volleyball.
if i uncheck checkbox1, It should show volleyball only.
if i check checkbox1,checkbox2,checkbox3, It should show all category ie, football, volleyball and bat in datalist.
if i uncheck checkbox2,checkbox3, it should show football only( checkbox1' s category).

checkbox1 (football)
checkbox2 (volleyball)
checkbox3 (bat)


how to do in asp.net c#. help needed.


if this performance is speed, asp.net c# is enough

or the help with javascript,c#,asp.net.
Posted
Comments
JoCodes 20-Nov-13 7:02am    
Anything you tried so far?
JoCodes 20-Nov-13 7:16am    
Post your datalist control markup code
You should try something. If you face any issues, then come back and improve the question with the details of the problem.
christhuxavier 20-Nov-13 7:26am    
okay i will try.

1 solution

Hi, I have solution myself. I would like to share. It may help others.

XML
aspx page:

<asp:CheckBoxList ID="chek" OnSelectedIndexChanged="chk_click" AutoPostBack="true" runat="server"></asp:CheckBoxList><br />

<!-- <asp:Button ID="btnclk" runat="server" Text="refersh" OnClick="chk_click" /><br />-->
<asp:DataList ID="dlchk" runat="server">
<HeaderTemplate>
<table width="600" align="center" cellpadding="10" cellspacing="10">


<tr>
<td>
Id
</td>
<td>
City
</td>
<td>
State
</td>
</tr>

</HeaderTemplate>

<ItemTemplate>
<tr>
<td>
<%#Eval("id") %>
</td>
<td>
<%#Eval("city") %>
</td>
<td>
<%#Eval("statename") %>
</td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:DataList>

aspx.cs (code behind):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace checkboxapp
{
public partial class _Default : System.Web.UI.Page
{
DBCLASS objcon = new DBCLASS();
DataTable dbdt = new DataTable();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
// Display the subtotal without tax when the page is first loaded.
if (!IsPostBack)
{
loaddt();

}

}
public void loaddt()
{
SqlConnection con = objcon.getconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from chktbl",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt);
Session["sesdt"] = dt;
//Filter using below line to uniqe data
dbdt = dt.DefaultView.ToTable(true,"id", "city");
con.Close();
chek.DataSource = dbdt;
chek.DataValueField = "id";
chek.DataTextField = "city";
// chek.DataTextField = "city";
chek.DataBind();
}
protected void chk_click(object sender, EventArgs e)
{
DataTable dtDatalist = new DataTable();
DataTable dtEachCity = new DataTable();
dt = (DataTable)Session["sesdt"];
DataRow[] dr;
foreach (ListItem listItem in chek.Items)
{
if (listItem.Selected)
{
//Get each selected city from checkboxlist and filter datatable baed on that and bind in datalist
dr = dt.Select("id='" + listItem.Value + "'");
if (dr.Length > 0)
{
dtEachCity = dr.CopyToDataTable();
dtDatalist.Merge(dtEachCity);
}
}
}
//Finally bind filtered data in the data list
dlchk.DataSource=dtDatalist;
dlchk.DataBind();

}
}
}

Thank you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900