How To Add Checkboxlist Dynamically in ASP.NET

ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services.

Checkbox list is a server-side component for rendering list of the checkbox on UI. In this article, I will show you how to add checkbox list dynamically in asp.net.
Check out the following code snippet, which is self-explanatory.
[]

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicControl.aspx.cs" Inherits="DynamicControl" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
<title>Untitled Page</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <table width="100%">  
            <col width="10%" />  
            <col width="90%" />  
            <tr>  
                <td>  
                </td>  
                <td>  
                    <asp:PlaceHolder ID="Ph" runat="server"></asp:PlaceHolder>  
                </td>  
            </tr>  
            <tr>  
                <td>  
                </td>  
                <td>  
                    <asp:Button ID="Btn" Text="[Add List]" runat="server" onclick="Btn_Click" />  
                </td>  
            </tr>  
        </table>  
    </div>  
    </form>  
</body>  
</html>  

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  

public partial class DynamicControl : System.Web.UI.Page  
{  
  static int intCount = 0;  
  protected void Page_Load(object sender, EventArgs e)  
  {  
      if (!Page.IsPostBack)  
      {  
          intCount++;  
          ViewState["ListCreated"] = false;  

      }  
      else  
      {  
          /// If list is created then display the values selected. This code portion can be moved  
          /// to the Btn_Click function in the else {} construct.  
          if (Convert.ToBoolean(ViewState["ListCreated"]))  
          {  
              DisplaySelection();  
          }  
      }  
  }  

  protected void Btn_Click(object sender, EventArgs e)  
  {  
      if (!Convert.ToBoolean(ViewState["ListCreated"]))  
      {  
          AddDynamicCheckboxList();  
      }  
  }  

  /// <summary>  
  /// AddDynamicCheckboxList method creates the checkboxlist dynamicall and add 5 items to it.  
  /// i.e. A, B, C, D, E. Once list is created ViewState["ListCreated"] will be set to true so  
  /// that it is redrawn again on the postback  
  /// </summary>  
  private void AddDynamicCheckboxList()  
  {  
      CheckBoxList CbxList = new CheckBoxList();  
      CbxList.ID = "Cbx";  
      for (int i = 0; i < intCount; i++)  
      {  
          CbxList.Items.Add(new ListItem(Convert.ToChar(i + 65).ToString(), Convert.ToChar(i + 65).ToString()));  
      }  
      Ph.Controls.Add(CbxList);  
      ViewState["ListCreated"] = true;  
  }  

  /// <summary>  
  /// DisplaySelection method is used to display the selected values once user presses the  
  /// button after selecting the checkboxes. It is now being called in the page_load method  
  /// but can also be called on the Btn_Click method int the else {} structure.  
  /// </summary>  
  private void DisplaySelection()  
  {  
      CheckBoxList Cbx = (CheckBoxList)Ph.FindControl("Cbx");  
      foreach (ListItem e in Cbx.Items)  
      {  
          if (e.Selected)  
          {  
              Response.Write(String.Format("You selected: <i>{0}</i> <br>", e.Value));  
          }  
      }  
  }  

  /// <summary>  
  /// LoadViewState method overridden so that we can check if the ListCreated is true then  
  /// we can redraw the controls.  
  /// </summary>  
  /// <param name="savedState"></param>  
  protected override void LoadViewState(object savedState)  
  {  
      base.LoadViewState(savedState);  

      if (Convert.ToBoolean(ViewState["ListCreated"]))  
      {  
          intCount++;  
          AddDynamicCheckboxList();  

      }  

  }  
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru