| | |
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:
Solved Threads: 0
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
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
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".
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".
ASP.NET Syntax (Toggle Plain Text)
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)); } }
Re: Check or uncheck items in a checkbox list, based on information from database
0
#3 Jul 14th, 2009
•
•
Join Date: Jun 2009
Posts: 435
Reputation:
Solved Threads: 82
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.
Re: Check or uncheck items in a checkbox list, based on information from database
0
#5 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"]; } }
yes i think ramesh is correct...
or you can use LISTITEM object to find out....thigns are checked/Unchecked ?
•
•
Join Date: Jul 2009
Posts: 69
Reputation:
Solved Threads: 0
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. =)
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. =)
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
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......
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......
![]() |
Similar Threads
- Retrieve Information from Database & Display into ASP.NET Web Controls Textbox (ASP.NET)
- check\uncheck all checkboxes in datalist control (ASP.NET)
- how to check items of checked list box ? (C#)
- insert checkbox list (VB.NET)
- a list of items (VB.NET)
- checkbox in datagridview c# (C#)
- Check/Uncheck All (Contact List) (DaniWeb Community Feedback)
- Programs not found in Add/Remove List (Windows NT / 2000 / XP)
Other Threads in the ASP.NET Forum
- Previous Thread: Redirect a page in a frame target
- Next Thread: website
| Thread Tools | Search this Thread |
.net 3.5 ajax alltypeofvideos appliances asp asp.net bc30451 beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions control countryselector dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iis javascript list listbox login microsoft mouse mssql nameisnotdeclared news novell numerical opera panelmasterpagebuttoncontrols problem radio redirect registration relationaldatabases reportemail save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos vista visualstudio vs2008 web webapplications webdevelopemnt webdevelopment webprogramming webservice wizard xsl youareanotmemberofthedebuggerusers






