Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.
I have a CheckBoxList to which I am populating data from the database and this is done under Page_Load event. Now I need to add a single CheckBox called SelectAll, so that when SelectAll CheckBox is checked all the items under CheckBoxList has to check without loading the page.
Thank You.
Posted
Comments
joshrduncan2012 26-Jan-15 14:36pm    
What have you tried? Where are you stuck?
partha143 26-Jan-15 15:21pm    
protected void Page_Load(object sender, EventArgs e)
{String sqlcon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;";
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
int cnt = Convert.ToInt32(cmd.ExecuteScalar());
if (cnt > 1)
{
SqlCommand cmd1 = new SqlCommand(query1, con);
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{
string myItem = dr["Sub_Code"].ToString();

CheckBoxList1.Items.Add(myItem);
}
}
else
{
SqlCommand cmd2 = new SqlCommand(query2, con);
SqlDataReader dr = cmd2.ExecuteReader();
while (dr.Read())
{
string myItem = dr["Sub_Code"].ToString();

CheckBoxList1.Items.Add(myItem);
}
}
con.Close();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}
}
I used LinkButton for SelectAll option. But as the LinkButton loads the page, The Page_Load event is executed again thereby producing the same data twice. So I need selectall option to work without loading the page.
Thank You.

One thing you have to add that the task of Page_Load event should be put under if(!IsPostBack) then your checkboxlist won't refill in each postback.So you will find everything work fine.However I have added a simple solution below

In ASPX

ASP
<![CDATA[<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<h3>Select Books</h3>
<div>
<asp:checkbox id="ckhSelectAll" runat="server" text="Select All" oncheckedchanged="ckhSelectAll_CheckedChanged" autopostback="true" checked="false" />
<asp:checkboxlist id="chkBookList" runat="server"></asp:checkboxlist>
</div>
</asp:content>

In .cs Code behind

C#
 BooksDBEntities db = new BooksDBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<book> lstBooks = new List<book>();
                lstBooks = db.Books.ToList();
                foreach (Book b in lstBooks)
                {
                    ListItem li = new ListItem();
                    li.Text = b.BookName;
                    li.Value = b.Id.ToString();
                    chkBookList.Items.Add(li);
                }
            }

        }

        protected void ckhSelectAll_CheckedChanged(object sender,EventArgs e)
        {
            if (ckhSelectAll.Checked)
            {
                foreach (ListItem l in chkBookList.Items)
                {
                    l.Selected = true;
                }
            }
            else
            {
                foreach (ListItem l in chkBookList.Items)
                {
                    l.Selected = false;
                }
            }
        }
</book></book>
 
Share this answer
 
v2
Comments
partha143 26-Jan-15 23:52pm    
Thanks a lot sir.
Anisuzzaman Sumon 27-Jan-15 0:16am    
No Mention :)
partha143 27-Jan-15 4:03am    
Sir, I have again stuck with checkboxlist. I am using 2 checkboxlist, 1 for names and 2nd for roll numbers. Is there any way from which I can select the roll number automatically when I check a name from 1st checkboxlist.
Thank You.
I think such type of problem can be faced easily if you use ListItem eg li li.Value and li.Text Property properly .However I don't know your case.I have attached a simple solution again for you by which you can select the roll number automatically when I check a name from 1st checkboxlist. and vice-versa 

In ASPX

ASP
<div>
<h3>Select Name </h3>
<asp:checkboxlist id="CheckBoxListName" autopostback="true" onselectedindexchanged="CheckBoxListName_SelectedIndexChanged" runat="server" xmlns:asp="#unknown"></asp:checkboxlist>
<h3>Select Roll</h3>
<asp:checkboxlist id="CheckBoxListRoll" autopostback="true" onselectedindexchanged="CheckBoxListRoll_SelectedIndexChanged" runat="server" xmlns:asp="#unknown"></asp:checkboxlist>
</div>


In .cs(Code behind)


C#
        BooksDBEntities db = new BooksDBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
 if (!IsPostBack)
            {
               
                //Two pair checkboxlist
                List<student> lstStudents = new List<student>();
                lstStudents = db.Students.ToList();
                //filling Names checkboxlist
                foreach (Student s in lstStudents)
                {
                    ListItem li = new ListItem();
                    li.Text = s.Name;
                    li.Value = s.Roll;

                    CheckBoxListName.Items.Add(li);
                }

                //filling Roll Checkboxlist

                foreach (Student s in lstStudents)
                {
                    ListItem li = new ListItem();
                    li.Text = s.Roll;
                    li.Value = s.Roll;
                    CheckBoxListRoll.Items.Add(li);
                }

                //


            }
}

protected void CheckBoxListName_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem li in CheckBoxListName.Items)
            {
               
                    CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;
          
            
            }
        
        }

        protected void CheckBoxListRoll_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (ListItem li in CheckBoxListRoll.Items)
            {
                CheckBoxListName.Items.FindByValue(li.Value).Selected = li.Selected;

            }
        }

</student></student>
 
Share this answer
 
Comments
partha143 27-Jan-15 7:03am    
Sir, firstly I would like to thank you for your speedy response. I am sorry as the issue I had written was not in a correct format which led you to misunderstand my issue. I will try my level best to make you understand. The 2 checkboxlist, one for Name and other for Roll_number. I am populating data for these checkboxlist from database and I have no problem doing that. So now there are 2 checkboxlists, so if I check the 1st name from the 1st checkboxlist(checkboxlistname) then the corresponding rollnumber from the 2nd checkboxlist(checkboxlistroll) has to check and vice-versa i.e. If I check the 1st name from 1st checkboxlist then the 1st roll_number has to be checked from the 2nd checkboxlist.
Thank You.
Anisuzzaman Sumon 27-Jan-15 23:26pm    
Yes I have understood your problem and it is the solution .May be you don't know about Entity framework however I am giving you a complete solution .You can download it. https://onedrive.live.com/redir?resid=1DCB770DAAE94B27%21222
Quote:
Let you have a table having data database table named Students as follows
ID   |	Roll     | Name
------------------------
1    |	0718004	 | Anis
------------------------
2    | 0718003   | Asad
-----------------------
3    | 0718002   | Toma
-----------------------

In aspx


ASP
<div>
<h3>Select Name </h3>
<asp:CheckBoxList ID="CheckBoxListName" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxListName_SelectedIndexChanged" runat="server"></asp:CheckBoxList>
<h3>Select Roll</h3>
<asp:CheckBoxList ID="CheckBoxListRoll" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxListRoll_SelectedIndexChanged" runat="server"></asp:CheckBoxList>
</div>


In .cs (Code behind) using Entity Framework


C#
public partial class _Default : System.Web.UI.Page
  {


    BooksDBEntities db = new BooksDBEntities();
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {

              List<Student> lstStudents = new List<Student>();
              lstStudents = db.Students.ToList();
              //filling Names checkboxlist
              foreach (Student s in lstStudents)
              {
                  ListItem li = new ListItem();
                  li.Text = s.Name;
                  li.Value = s.Roll;

                  CheckBoxListName.Items.Add(li);
              }

              //filling Roll Checkboxlist

              foreach (Student s in lstStudents)
              {
                  ListItem li = new ListItem();
                  li.Text = s.Roll;
                  li.Value = s.Roll;
                  CheckBoxListRoll.Items.Add(li);
              }
              //


          }

      }

       protected void CheckBoxListName_SelectedIndexChanged(object sender, EventArgs e)
      {
          foreach (ListItem li in CheckBoxListName.Items)
          {

                  CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;


          }

      }

      protected void CheckBoxListRoll_SelectedIndexChanged(object sender, EventArgs e)
      {

          foreach (ListItem li in CheckBoxListRoll.Items)
          {
              CheckBoxListName.Items.FindByValue(li.Value).Selected = li.Selected;

          }
      }


      }


Using above code if you select a roll number say 0718004 then the in checkboxlist for Name will automatically select Anis and vice versa. I have link wherein I provided the complete solution you can download. If it works then upvote and accept as answer 

Download the project here

Thanks









 
Share this answer
 
v2

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



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