Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I written following dynamic code for call check box CheckChanged event.
But I unable to call this event.

Please, give me help what is the actual issue?



CheckBox chk = new CheckBox();

string uniqueID = System.Guid.NewGuid().ToString().Substring(0, 5);
chk.ID = "chktrait" + cn.AspectID + "_" + cn.TraitID + uniqueID;
chk.Text = cn.ValueName;
chk.AutoPostBack = true;
chk.CheckedChanged += new EventHandler(CheckBox_CheckChanged);



protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
//write the client id of the control that triggered the event
Response.Write(((CheckBox)sender).ClientID);
}
Posted

1 solution

Here is an example to check your scenario and it works fine after adding the checkbox control to the form in current page.

C#
protected void Page_Load(object sender, EventArgs e)
        {
            CheckBox chk = new CheckBox();

            string uniqueID = System.Guid.NewGuid().ToString().Substring(0, 5);
            chk.ID = "chkTest";
            chk.Text = "Test";
            chk.AutoPostBack = true;
            chk.CheckedChanged += new EventHandler(CheckBox_CheckChanged);
            this.form1.Controls.Add(chk);
        }

        protected void CheckBox_CheckChanged(object sender, EventArgs e)
        {
            //write the client id of the control that triggered the event
            lblTest.Text = ((CheckBox)sender).ClientID;
        }


Hope this will be of help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900