In my markup (Default.aspx), I have a simple table:
<asp:Table id="myAspTable" runat="server" />
I my code behind (Default.aspx.cs), I have an integer (n) which can be anything from 1 to 100:
int n = getValueOfN();
Based on this number, I can create checkboxes and textboxes dynamically and add them to my page:
CheckBox[] checks = new CheckBox[n];
TextBox[] texts = new TextBox[n];
for (int i=0; i<n; i++)
{
checks[i] = new CheckBox();
texts[i] = new TextBox();
tblrow = new TableRow();
tblcell = new TableCell();
tblcell.Controls.Add(checks[i]);
tblcell.Controls.Add(texts[i]);
tblrow.Controls.Add(tblcell);
myAspTable.Controls.Add(tblrow);
}
I now want to add the following functionality: Each checkbox i must enable or disable textbox i, when checked/unchecked respectively. How do I do this 100% in the code-behind?
Here is what I have tried:
checks[i].AutoPostBack = true;
checks[i].CheckedChanged += new EventHandler(this.CheckToggleEnable);
public void CheckToggleEnable(object sender, EventArgs e)
{
// Implementation here
}
But this doesn't work because I have no way of referencing Textbox i in the CheckToggleEnable function. Also, I was hoping to do this without a post-back.