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

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".

private List<Book> selections;
	
	private void GridDataBind(int id)
    {
        try
        {
			User user = (from u in db.Users
						 where u.Id == id
						 select u).Single();
						
			// GetUserBooks() represents your logic to find the user's books
			List<Book> userBooks = GetUserBooks(user.Id);
			
	   // Assigns the books to a viewstate variable.
            ViewState["books"] = userBooks;
			
	   // Order the binding of the grid
	   // This will trigger the gvBooks_RowDataBound()
            gvBooks.DataBind();
        }
        catch (Exception ex)
        {
			throw;
        }
    }

	protected void gvBooks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chxBooks = (CheckBox)e.Row.FindControl("cbEntries");

	    // Receive the ViewState assigned @ GridDataBind()
            selections = (List<Book>)ViewState["books"];
			
           // Checks everyone contained in the list
            if (selections != null)
                chxBooks.Checked = selections.Contains(((Book)e.Row.DataItem));
        }
    }

Ana D.
Did you work out on that issue? Which database are using? Post your code if you have.

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"];


    }
}

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 ?

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. =)

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......

This question has already been answered. Start a new discussion instead.