Friday, 29 August 2014

How To Generate Dynamic Controls in Asp.net.

Introduction:
Here I will explain how to generate a controls dynamically on button click.
Description:
By Using Place-Holder control I am going to generate Check box controls on the button click.
And How To Dynamically delete the controls.

  PlaceHolder.aspx

  • Steps1: Take PlaceHolder controls.
  • Step2:Take1 Text box to generate no of dynamic controls.
  • Step3:Take 1 button 
PlaceHolder.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class PlaceHolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < int.Parse(TextBox1.Text.ToString()); i++)
        {
         CheckBox chk = new CheckBox();
          PlaceHolder1.Controls.Add(chk);
          Button rdb = new Button();
            rdb.Text = "Delete";
            PlaceHolder1.Controls.Add(rdb);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));    
        }
        }
        }



Give the no you want to generate controls dynamically.

After Generate the controls if u want to delete click on the delete button to delete dynamically generated buttons.

0 comments::

Post a Comment

Copyright © DotNet-Ashok