Dynamic Control and Event handling

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.
 
S

Scott Roberts

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());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top