How To Add Checkboxlist Dynamically




<%@ 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();

}

}
}

3 comments:

  1. Thanks for posting. its really help me alot...

    ReplyDelete
  2. Thanks for Your post,
    i have one doubt on create control on dynamic what is that, now your used CheckBoxList CbxList = new CheckBoxList(); so it will create s single control is it possible for create multiple controls with different id in a singleclick ?

    ReplyDelete
  3. Thanks for your post, i have one doubt on create multiple control on single button click with multiple Id is it possible ?

    ReplyDelete