dynamically created checkbox event not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yogarajan
    New Member
    • Apr 2007
    • 115

    dynamically created checkbox event not working

    Hi All

    this is my page load event
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i <= 3; i++)
            {
    
                TableRow tr = new TableRow();
                TableCell td1 = new TableCell();
                td1.Text = "1";
                TableCell td2 = new TableCell();
                td2.Text = "2";
                tr.Cells.Add(td1);
                tr.Cells.Add(td2);
                Table1.Rows.Add(tr);
                TableRow tr2 = new TableRow();
                TableCell td3 = new TableCell();
                td3.ColumnSpan = 2;
                CheckBox chk = new CheckBox();
                chk.ID = Convert.ToString(i);
                chk.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                td3.Controls.Add(chk);
                tr2.Cells.Add(td3);
                Table1.Rows.Add(tr2);
            }
    
        }
    
    
    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
    
            Label1.Text = chk.Checked ? String.Format("You checked CheckBox '{0}'.", chk.ID) : String.Format("You unchecked CheckBox '{0}'.", chk.ID);
            //Label1.Visible = true;
        }
    this is my code
    but CheckBox_Checke dChanged event not working

    pls help
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Did you create it as a server object? One of the risks of creating objects dynamically...

    Comment

    • yogarajan
      New Member
      • Apr 2007
      • 115

      #3
      Hi

      do u have any sample
      pls give me

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by yogarajan
        Hi

        do u have any sample
        pls give me

        I think that you are going to have to declare your checkbox elsewhere....I' m not sure how to use events with C#, but check out this article on how to use dynamic controls in asp.net...

        -Frinny

        Comment

        Working...