Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a dirty solution I used for the moment (get it working then get it right, right?).</p> <p>I had already created a new class that inherits the UserControl class and from which all other "UserControls" I created were derived. I called it formPartial (nod to Rails), and this is going inside the public string renderMyHTML() method:</p> <pre><code>TextWriter myTextWriter = new StringWriter(); HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter); UserControl myDuplicate = new UserControl(); TextBox blankTextBox; foreach (Control tmpControl in this.Controls) { switch (tmpControl.GetType().ToString()) { case "System.Web.UI.LiteralControl": blankLiteral = new LiteralControl(); blankLiteral.Text = ((LiteralControl)tmpControl).Text; myDuplicate.Controls.Add(blankLiteral); break; case "System.Web.UI.WebControls.TextBox": blankTextBox = new TextBox(); blankTextBox.ID = ((TextBox)tmpControl).ID; blankTextBox.Text = ((TextBox)tmpControl).Text; myDuplicate.Controls.Add(blankTextBox); break; // ...other types of controls (ddls, checkboxes, etc.) } } myDuplicate.RenderControl(myWriter); return myTextWriter.ToString(); </code></pre> <p>Drawbacks off the top of my head:</p> <ol> <li>You need a case statement with every possible control (or controls you expect).</li> <li>You need to transfer all the important attributes from the existing control (textbox, etc) to the new blank control.</li> <li>Doesn't take full advantage of Controls' RenderControl method.</li> </ol> <p>It'd be easy to mess up 1 or 2. Hopefully, though, this helps someone else come up with a more elegant solution.</p>
 

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