Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dynamic Controls are whole other beast and I try to stay away from them as much as possible. That being said, there are cases where they may be necessary. </p> <p>The thing about event handlers and dynamic controls, you have to be sure to wire up and add the control to the page control tree in the OnInit or Page_Load functions otherwise they can behave strangely <a href="http://support.microsoft.com/?id=317794" rel="nofollow">http://support.microsoft.com/?id=317794</a></p> <p>I've made some edits to your code that wires up the event handlers at Page_Load. See below and let me know if you have any questions:</p> <pre><code> // Added a ViewState persisted prop bool WireUpControls { get { return ViewState["wireUpControls"] != null ? Convert.ToBoolean(ViewState["wireUpControls"]) : false; } set { ViewState["wireUpControls"] = value; } } void CreateControlTable() { int cellIx = 0, rowIx = 0; HtmlTableRow tr1 = new HtmlTableRow(); HtmlTableCell td11 = new HtmlTableCell(); tr1.Cells.Add(td11); HtmlTableCell td12 = new HtmlTableCell(); td12.InnerText = "td12"; tr1.Cells.Add(td12); HtmlTableRow tr2 = new HtmlTableRow(); HtmlTableCell td21 = new HtmlTableCell(); tr2.Cells.Add(td21); HtmlTableCell td22 = new HtmlTableCell(); tr2.Cells.Add(td22); td22.InnerText = "td22"; HtmlTableRow[] arrRows = new HtmlTableRow[2]; arrRows[rowIx++] = tr1; arrRows[rowIx++] = tr2; HtmlTableCell[] arrCells = new HtmlTableCell[4]; arrCells[cellIx++] = td11; arrCells[cellIx++] = td12; arrCells[cellIx++] = td21; arrCells[cellIx++] = td22; ControlInfo ci = PersistControl("tblResult", "HtmlTable", 3, arrRows, arrCells, 2, 2); HtmlTable tblResult = (HtmlTable)CreateControl(ci); ci = PersistControl("btnEdit1", "Button", 0, arrRows, arrCells, 2, 2); Button btnEdit1 = (Button)CreateControl(ci); ci = PersistControl("btnEdit2", "Button", 0, arrRows, arrCells, 2, 2); Button btnEdit2 = (Button)CreateControl(ci); } // Call CreateControlTable() if we need to create the controls and wire up events protected void Page_Load(object sender, EventArgs e) { btnCreateTbl.Click += new EventHandler(BtnClick); if (WireUpControls) CreateControlTable(); //if (this.IsPostBack) this.RecreatePersistedControls(); } protected void BtnClick(object sender, EventArgs e) { WireUpControls = true; CreateControlTable(); } public void DoNothing(object sender, EventArgs e) { lblResult.Text = (sender as Button).ID + " done"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload