Add Dynamic CheckBox Handle CheckedChanged Event ASP.NET

Add Dynamic CheckBox And Handle CheckedChanged Event In ASP.NET 2.0,3.5,4.0 Using C# And VB.NET. Several times we need to add dynamic controls in asp.net application using code behind.

In this post i am going to describe how to add dynamic CheckBox and assign CheckChanged event in asp.net using C# and VB.NET


To add a checkbox when page is loaded we need to write this code in page_Load Event

C# Code Behind

public partial class _Default : System.Web.UI.Page 
{
    CheckBox chkDynamic;
    protected void Page_Load(object sender, EventArgs e)
    {
        chkDynamic = new CheckBox();
        chkDynamic.ID = "chkExample";
        chkDynamic.Text = "Check / Uncheck";
        chkDynamic.AutoPostBack = true;
        chkDynamic.CheckedChanged += new EventHandler
                           (chkDynamic_CheckedChanged);
        this.Form.Controls.Add(chkDynamic);
    }

    protected void chkDynamic_CheckedChanged
                 (object sender, EventArgs e)
    {
        if (chkDynamic.Checked)
            lblMessage.Text = "you checked the checkbox";
        else if (!chkDynamic.Checked)
            lblMessage.Text = "checkbox is not checked";
    }
}

VB.NET Code Behind
Public Partial Class _Default
    Inherits System.Web.UI.Page
    Private chkDynamic As CheckBox
    Protected Sub Page_Load
    (ByVal sender As Object, ByVal e As EventArgs)
        chkDynamic = New CheckBox()
        chkDynamic.ID = "chkExample"
        chkDynamic.Text = "Check / Uncheck"
        chkDynamic.AutoPostBack = True
        AddHandler chkDynamic.CheckedChanged, 
         AddressOf chkDynamic_CheckedChanged
        Me.Form.Controls.Add(chkDynamic)
    End Sub
    
    Protected Sub chkDynamic_CheckedChanged
    (ByVal sender As Object, ByVal e As EventArgs)
        If chkDynamic.Checked Then
            lblMessage.Text = "you checked the checkbox"
        ElseIf Not chkDynamic.Checked Then
            lblMessage.Text = "checkbox is not checked"
        End If
    End Sub
End Class

Hope this helps

Read other articles on checkboxes:
Edit or update multiple records/rows in gridview with checkbox
Delete multiple rows records in Gridview with checkbox and confirmation
If you like this post than join us or share

12 comments:

Anonymous said...

this is not working


Anonymous said...

yeah, not working!


myros said...

try this

If (sender).Checked Then
lblMessage.Text = "you checked the checkbox" & " " & sender.id
ElseIf Not sender.Checked Then
lblMessage.Text = "checkbox is not checked"
End If


Anonymous said...

This is not working because the event handler is attached in Page_Load. You need to do this on Page_Init


Anonymous said...

It is working but not for more than one check boxes.If form has only one working and if form has 2 check boxes event firing for last check box


Anonymous said...

working for single check box if we declare more than one check box for the last box the event firing and I checked the id,for each box event got fired but showing only the last check box id and if it is checked then only msg changing


Anonymous said...

don't forget to enable the autopostback property


Anonymous said...

Its working correctly
Just check it...


Manish said...

thnx for the code........


Anonymous said...

not working
only last checkbox is showing msg.....


Anonymous said...

I use the VB.net and it works for me. I use VS 3.5. Thanks for info.


Shopholic said...

thanks men for solutions .... i was tired with this problems from the last few days.


Find More Articles