SIGN UP MEMBER LOGIN:    
ARTICLE

How to generate Dynamic Checkbox in ASP.Net using C#

Posted by Sushmita Kumari Articles | ASP.NET Programming January 11, 2006
After completion of this article we will be in a position to understand the approach for generating Dynamic checkboxes in Asp.Net using c#.net as well as the uses of Dynamic control.
Reader Level:

Introduction

 

As a programmer we need to know about the static as well as dynamic controls. It's not necessary that always we will be using the static controls. Many programmers are having a mind setup like if they want the dynamic controls they have to write many lines of code but it's not like that.

 

Suppose we want to add dynamic control to a Panal control.So, what is the approach for that?

Following code will explain how to create the dynamic checkboxes.

 

Source Code:

 

//Drag and drop panal on the Webpage.

//After drag and drop following code will be generated automatically

protected System.Web.UI.WebControls.Panel PnlControl;

//Declare a CheckBox

CheckBox chkList1;

private void Page_Load(object sender, System.EventArgs e)

          {

                .

                .

                //Page load code goes here

                .

                .

          }

#region dyanamic checkboxes

//Function to generate the dyanamic checkboxes

private void AddCheckboxes(string strCheckboxText)

          {

                try

                {

                     if(PnlTimeExpence.HasControls())

                     {

                           return;

                     }

                     for(int intControlIndex=0;intControlIndex=5;intControlIndex++)

                     {

                           chkList1 =new CheckBox();

                           chkList1.Text = strCheckboxText;

                           chkList1.ID="Chk"+intControlIndex;

                           chkList1.Font.Name = "Verdana";

                           chkList1.Font.Size = 9;

                           PnlControl.Controls.Add(chkList1);

                           PnlControl.Controls.Add(new LiteralControl("<br>"));

                     }

                }

                catch(Exception exp)

                {

                     throw new Exception(exp.Message);

                }

          }

#endregion

Login to add your contents and source code to this article
share this article :
post comment
 

Hi

 

in C#

private void Page_Load(object sender, System.EventArgs e)

{

CheckBox cb = new CheckBox();

cb.ID = "cb";

cb.Width=50;

cb.Height=90;

cb.Text="ENTER";

cb.Click +=new EventHandler(cb_Click);

Panel1.Controls.Add(btn);

//Panel1.Controls.Add(br);

}

}

void cb_Click(object sender, System.EventArgs e)

{

Response.Write("<script> alert('hi khalid'); </script>");

}

Posted by khalid sharief Aug 27, 2008

Hello...this article is good to generate dynamic checkboxes but i have 1 problem in that i have form for blog details and in that for category field i have to generate dynamic checkboxes according to the categorieds avaible in my database so how can i manage space for that along with all my details...can u help me...

Posted by chandni punjabi Mar 23, 2008

how to write event for checkbox to be created at runtime.

Posted by PREM ANAND Mar 02, 2007
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor