Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » ASP.NET WebForms »
Maintain Checked State for CheckBoxList in ASP.NET
|
Maintain Checked State for CheckBoxList in ASP.NET
Introduction We will see how to maintain the checked state of a CheckBoxList control among page redirections. That is, moving from a page containing a CheckBoxList control to another page, restoring the checked state of the check boxes in the CheckBoxList when coming back to the same page.
CheckBoxList Control The CheckBoxList control in ASP.NET allows users to select more than one option in a web page. It provides a multi selection check box group that can be dynamically generated with data binding. Assume that you have a web page in which you ask the users to select their book topics from a list of options or select product details that the users are interested to buy them. In scenario, we can use CheckBoxList control to provide a better user interface. Retrieving the data source and binding to a CheckBoxList is same as with GridView control in ASP.NET.
The checked items state in a CheckBoxList control will automatically be maintained between postbacks if EnableViewState is set to true. But the state will not be maintained if you move to another page and come back to the original page.
Logic To maintain the state when moving to other pages, the following logic has been used in this code snippet.
1. Before moving to another page, the values of the selected items in the CheckBoxList are concatenated using comma delimiter and store it in the session.
2. After coming back to this page from other pages, retrieve the session variable , split the values, Find items by value in the CheckBoxList and select them.
Code Snippet – aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage30.aspx.cs" Inherits="DemoPage30" %>
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table width="100%"> <tr> <td> <asp:CheckBoxList ID="cblBooks" runat="server"> </asp:CheckBoxList> </td> </tr> <tr> <td> <asp:Button ID="btnNextPage" runat="server" onclick="btnNextPage_Click" Text="Next Page" /> </td> </tr> </table> </div> </form> </body> </html>
Code Snippet – C#
using System; using System.Data; using System.Web.UI.WebControls;
public partial class DemoPage30 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindCheckBoxList(); } } private void BindCheckBoxList() {
//You can load the following DataTable from a database DataTable dtBooks = new DataTable();
dtBooks.Columns.Add("Book_Id", System.Type.GetType("System.Int32")); dtBooks.Columns.Add("Book_Name", System.Type.GetType("System.String"));
dtBooks.Rows.Add(new object[] { 100, "ASP.NET Programming" }); dtBooks.Rows.Add(new object[] { 101, "Beginning Web Development" }); dtBooks.Rows.Add(new object[] { 102, "ASP.NET for Dummies" }); dtBooks.Rows.Add(new object[] { 103, "SharePoint 2007" }); dtBooks.Rows.Add(new object[] { 104, "PHP Programming" });
//Binding DataTable to the CheckBoxList control cblBooks.DataSource = dtBooks; cblBooks.DataTextField = "Book_Name"; cblBooks.DataValueField = "Book_Id"; cblBooks.DataBind();
//Select the list items based on the session value if (Session["BooksSelected"] != null) { string[] bookItems = Session["BooksSelected"].ToString().Split(','); foreach (string bookId in bookItems) { ListItem item = cblBooks.Items.FindByValue(bookId); if (item != null) item.Selected = true; } } }
protected void btnNextPage_Click(object sender, EventArgs e) { //Store the selected item values in Session before moving to //another page String itemSelected = String.Empty; foreach (ListItem item in cblBooks.Items) { if (item.Selected) itemSelected = itemSelected + item.Value + ",";
} itemSelected = itemSelected.TrimEnd(','); Session["BooksSelected"] = itemSelected; Response.Redirect("ViewBooks.aspx"); } }
ListBox Since the Items are populated as ListItem object in CheckBoxList and ListBox controls, the same code can be used to maintain state of a multi-selectable ListBox in ASP.NET.
AttachmentsMaitain State CheckBoxList (34228-20619-Maitain_State_CheckBoxList.zip)
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|