Dynamic Control and Event handling

  • Thread starter Thread starter DotNetDev
  • Start date Start date
D

DotNetDev

Hi,

I add some checkboxes to an <ASP.Table>

I want execute a function whenever one of the checkboxes is clicked.

How can I do that.

This is how I was trying...
TableRow aRow = new TableRow();
CheckBox aBox = new CheckBox();
aBox.ID = "SomeID";
aBox.CheckedChanged += new EventHandler(aBox_CheckedChanged);
aBox.AutoPostBack = true;
TableCell aCell = new TableCell();
aCell.Controls.Add(aBox);
aRow.Cells.Add(aCell);
aTable.Rows.Add(aRow);

where aTable is a 'System.Web.UI.WebControls.Table'

Thnx for your help.
 
Works fine for me:

protected void Page_Load(object sender, EventArgs e)
{
TableRow aRow = new TableRow();
CheckBox aBox = new CheckBox();
aBox.ID = "SomeID";
aBox.CheckedChanged += new EventHandler(aBox_CheckedChanged);
aBox.AutoPostBack = true;
TableCell aCell = new TableCell();
aCell.Controls.Add(aBox);
aRow.Cells.Add(aCell);
Table1.Rows.Add(aRow);
}

private void aBox_CheckedChanged(object sender, EventArgs e)
{
Console.WriteLine((sender as CheckBox).Checked.ToString());
}
 
Back
Top