Check or uncheck items in a checkbox list, based on information from database

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Check or uncheck items in a checkbox list, based on information from database

 
0
  #1
Jul 13th, 2009
Hi,

I have a checkbox list with a list of books. Depending on the user, some of these books were already chosen. Let's suppose that I have a list of the following books:
Programming ASP.NET
Learning ASP.NET
Beginning Web Development
ASP.NET for Dummies

But the books the user A chose were Programming ASP.NET and Beginning Web Development. In this case, I have to show the list of the five books, but only the books the user chose will be checked.
Anyone have any idea about how do I get the information about the books chosen from the database and check them in the CheckBox List?

Thanks,

Ana
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 10
Reputation: rafaelbelliard is an unknown quantity at this point 
Solved Threads: 4
rafaelbelliard's Avatar
rafaelbelliard rafaelbelliard is offline Offline
Newbie Poster

Re: Check or uncheck items in a checkbox list, based on information from database

 
0
  #2
Jul 13th, 2009
What you would want to do is call an event which would get all the books for a given user, and then make that event check all the controls.

In this example, my GridDataBind receives the user's ID, fetches the list of books, then triggers my gridview's databound event, which looks for a checkbox called "cbEntries".

  1. private List<Book> selections;
  2.  
  3. private void GridDataBind(int id)
  4. {
  5. try
  6. {
  7. User user = (from u in db.Users
  8. where u.Id == id
  9. select u).Single();
  10.  
  11. // GetUserBooks() represents your logic to find the user's books
  12. List<Book> userBooks = GetUserBooks(user.Id);
  13.  
  14. // Assigns the books to a viewstate variable.
  15. ViewState["books"] = userBooks;
  16.  
  17. // Order the binding of the grid
  18. // This will trigger the gvBooks_RowDataBound()
  19. gvBooks.DataBind();
  20. }
  21. catch (Exception ex)
  22. {
  23. throw;
  24. }
  25. }
  26.  
  27. protected void gvBooks_RowDataBound(object sender, GridViewRowEventArgs e)
  28. {
  29. if (e.Row.RowType == DataControlRowType.DataRow)
  30. {
  31. CheckBox chxBooks = (CheckBox)e.Row.FindControl("cbEntries");
  32.  
  33. // Receive the ViewState assigned @ GridDataBind()
  34. selections = (List<Book>)ViewState["books"];
  35.  
  36. // Checks everyone contained in the list
  37. if (selections != null)
  38. chxBooks.Checked = selections.Contains(((Book)e.Row.DataItem));
  39. }
  40. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 467
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Check or uncheck items in a checkbox list, based on information from database

 
0
  #3
Jul 14th, 2009
Ana D.
Did you work out on that issue? Which database are using? Post your code if you have.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 435
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training

Re: Check or uncheck items in a checkbox list, based on information from database

 
0
  #4
Jul 14th, 2009
Use this sample code

using System;
using System.Data;

public partial class DemoCheckBoxList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Bind CheckBoxList on page load
        if (!IsPostBack)
            BindCheckBoxList();
    }

    private void BindCheckBoxList()
    {

        //You can load the following DataTable from a database
        DataTable dtBooks = new DataTable();

        dtBooks.Columns.Add("Is_Chosen", System.Type.GetType("System.Boolean"));
        dtBooks.Columns.Add("Book_Name", System.Type.GetType("System.String"));

        dtBooks.Rows.Add(new object[] { true, "ASP.NET Programming" });
        dtBooks.Rows.Add(new object[] { true, "Beginning Web Development" });
        dtBooks.Rows.Add(new object[] { false, "ASP.NET for Dummies" });
        dtBooks.Rows.Add(new object[] { true, "SharePoint 2007" });
        dtBooks.Rows.Add(new object[] { false, "PHP Programming" });

        //Binding DataTable to the CheckBoxList control
        cblBooks.DataSource = dtBooks;
        cblBooks.DataTextField = "Book_Name";
        cblBooks.DataValueField = "Is_Chosen";
        cblBooks.DataBind();

        //Select/Unselect checkboxes based on the value of 'Is_Chosen' boolean column 
        //of the DataTable
        for (int i = 0; i < dtBooks.Rows.Count; i++)
            cblBooks.Items[i].Selected = (Boolean)dtBooks.Rows[i]["Is_Chosen"];


    }
}
Last edited by Ramesh S; Jul 14th, 2009 at 2:01 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 164
Reputation: dnanetwork has a little shameless behaviour in the past 
Solved Threads: 24
dnanetwork's Avatar
dnanetwork dnanetwork is offline Offline
Junior Poster

Re: Check or uncheck items in a checkbox list, based on information from database

 
0
  #5
Jul 14th, 2009
Originally Posted by Ramesh S View Post
Use this sample code

using System;
using System.Data;

public partial class DemoCheckBoxList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Bind CheckBoxList on page load
        if (!IsPostBack)
            BindCheckBoxList();
    }

    private void BindCheckBoxList()
    {

        //You can load the following DataTable from a database
        DataTable dtBooks = new DataTable();

        dtBooks.Columns.Add("Is_Chosen", System.Type.GetType("System.Boolean"));
        dtBooks.Columns.Add("Book_Name", System.Type.GetType("System.String"));

        dtBooks.Rows.Add(new object[] { true, "ASP.NET Programming" });
        dtBooks.Rows.Add(new object[] { true, "Beginning Web Development" });
        dtBooks.Rows.Add(new object[] { false, "ASP.NET for Dummies" });
        dtBooks.Rows.Add(new object[] { true, "SharePoint 2007" });
        dtBooks.Rows.Add(new object[] { false, "PHP Programming" });

        //Binding DataTable to the CheckBoxList control
        cblBooks.DataSource = dtBooks;
        cblBooks.DataTextField = "Book_Name";
        cblBooks.DataValueField = "Is_Chosen";
        cblBooks.DataBind();

        //Select/Unselect checkboxes based on the value of 'Is_Chosen' boolean column 
        //of the DataTable
        for (int i = 0; i < dtBooks.Rows.Count; i++)
            cblBooks.Items[i].Selected = (Boolean)dtBooks.Rows[i]["Is_Chosen"];


    }
}

yes i think ramesh is correct...

or you can use LISTITEM object to find out....thigns are checked/Unchecked ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Re: Check or uncheck items in a checkbox list, based on information from database

 
0
  #6
Jul 14th, 2009
Thanks to all for the replies. The way I solved the problem was:
1) I populated my checkBoxList with the information from the DataBase
2) Created an ArrayList with the Books selected by the user (I retrieved the information from the database and, using an SqlDataReader stored the infomartion in the ArrayList)
3) Compared each item in the CheckBoxList with the items in the ArrayList. In case of the item in the CheckBoxList be in the ArrayList, the CheckBoxList.Items(index).Selected = True

I decided to post the solution in case of someone has the same question in the future. =)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: alexymasilamani is an unknown quantity at this point 
Solved Threads: 0
alexymasilamani alexymasilamani is offline Offline
Newbie Poster

Help pls

 
0
  #7
18 Days Ago
Hay Can some one do me favour???????
I need complete code for Check/Uncheck all items in checkboxlist using asp.net in serverside.
I kept one checkboxlist and bound data source to it in runtime and using two linkbutton to check or uncheck items in checkboxlist.i need to reterive data from selected checkboxlist.
i want this in server side code not by javascript.
I tried this code in linkbutton click event but no hope.
foreach(ListItem item in checkboxlist1.Items)
{
item.selected=true;
}
but no hope:-( pls some one help me......
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC