Create CheckBox Dynamically in ASP.NET


how to create checkbox dynamically in asp.net

Hello Friends,
In these Tutorials we see how to create checkbox dynamically when we click button its check textbox to display number of CheckBox's.

The below code will help you to Create Dynamically CheckBox in Asp.net

HTML Markup : Create Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Create-Dynamic-Checkbox-RadioButton.aspx.cs" Inherits="Example_Create_Dynamic_Checkbox_RadioButton" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Create CheckBox" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br /><br />
    How Many CheckBox|RadioButton Wants to Create : <asp:TextBox ID="txt" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Create Controll" 
            onclick="Button1_Click" /><br /><br /><br />
            <asp:panel ID="Panel1" runat="server"></asp:panel>

    </div>    
    </form>
</body>
</html>

C# : Code for Create CheckBox Dynamically

protected void Button1_Click(object sender, EventArgs e)
{
 if (CheckBox1.Checked && txt.Text!="")
        {
            int count = Convert.ToInt32(txt.Text);
            for (int i = 0; i < count; i++)
            {
                CheckBox chk = new System.Web.UI.WebControls.CheckBox();
                chk.ID = "chk" + i;
                chk.Text = "CheckBox" + (i+1) + "
"; Panel1.Controls.Add(chk); } } }
Tag : ASP.NET, CheckBox
0 Komentar untuk "Create CheckBox Dynamically in ASP.NET"

Back To Top