Friday, August 10, 2012

How to create dynamic checkbox

Html code

First add a Table in the html

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to create dynamic checkbox</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="tblDynamic" runat="server" Width="300" Height="200">
        </asp:Table>
    </div>
    </form>
</body>
</html>

c# code


protected void Page_Load(object sender, EventArgs e)
    {
        // Add table row 
        TableRow row1 = new TableRow();
        
        // Add table cell  
        TableCell checkboxcell = new TableCell();

        CheckBoxList Checkbox = new CheckBoxList();
        Checkbox.ID = "CheckboxTitle";
        
        for (int j = 1; j <= 4; j++)
        {
            // To create CheckBox
            Checkbox.Items.Add("Option" + j);
            Checkbox.EnableViewState = false;
        }
        checkboxcell.Controls.Add(Checkbox);
        row1.Cells.Add(checkboxcell);
        
        // add the row to the table in html  
        tblDynamic.Rows.Add(row1);
    }


No comments:

Post a Comment