August 29, 2008 by Christoff Truter C# ASP.NET
Creating composite controls in ASP.net can prove to be quite
a tedious process at times, when it comes to the actual HTML the control renders.
Especially since design-time support for composite controls are quite limited.
Consider the following piece of HTML, how would one go to work to render this from a control?
<table>
<tr>
<td>
<input name=" txtUrl" type="text" id=" txtUrl" style="width:470px;" value="http://" />
</td>
<td>
<input type="submit" name="btnDisplay" value="Go" id=" btnDisplay" />
</td>
</tr>
<tr>
<td colspan="2">
<iframe id="ifBrowser" width="500px" height="500px"></iframe>
</td>
</tr>
</table>
protected Button btnDisplay = new Button();
protected TextBox txtUrl = new TextBox();
protected HtmlGenericControl ifBrowser = new HtmlGenericControl("iframe");
protected override void Render(HtmlTextWriter writer)
{
writer.WriteFullBeginTag("table");
writer.WriteFullBeginTag("tr");
writer.WriteFullBeginTag("td");
txtUrl.ID = "txtUrl";
txtUrl.Text = "http://";
txtUrl.Width = new Unit(470);
txtUrl.RenderControl(writer);
writer.WriteEndTag("td");
writer.WriteFullBeginTag("td");
btnDisplay.ID = "btnDisplay";
btnDisplay.Text = "Go";
btnDisplay.CommandName = "Click";
btnDisplay.RenderControl(writer);
writer.WriteEndTag("td");
writer.WriteEndTag("tr");
writer.WriteFullBeginTag("tr");
writer.WriteBeginTag("td");
writer.WriteAttribute("colspan", "2");
writer.Write(">");
ifBrowser.ID = "ifBrowser";
ifBrowser.Attributes["width"] = "500px";
ifBrowser.Attributes["height"] = "500px";
ifBrowser.RenderControl(writer);
writer.WriteEndTag("td");
writer.WriteEndTag("tr");
writer.WriteEndTag("table");
base.Render(writer);
}protected Button btnDisplay = new Button();
protected TextBox txtUrl = new TextBox();
protected HtmlGenericControl ifBrowser = new HtmlGenericControl("iframe");
protected override void CreateChildControls()
{
HtmlTable mainTable = new HtmlTable();
HtmlTableRow firstRow = new HtmlTableRow();
HtmlTableRow secondRow = new HtmlTableRow();
mainTable.Controls.Add(firstRow);
mainTable.Controls.Add(secondRow);
HtmlTableCell firstRowFirstCell = new HtmlTableCell();
txtUrl.ID = "txtUrl";
txtUrl.Text = "http://";
txtUrl.Width = new Unit(470);
firstRowFirstCell.Controls.Add(txtUrl);
HtmlTableCell firstRowSecondCell = new HtmlTableCell();
btnDisplay.ID = "btnDisplay";
btnDisplay.Text = "Go";
btnDisplay.CommandName = "Click";
firstRowSecondCell.Controls.Add(btnDisplay);
firstRow.Controls.Add(firstRowFirstCell);
firstRow.Controls.Add(firstRowSecondCell);
HtmlTableCell secondRowfirstCell = new HtmlTableCell();
ifBrowser.Attributes["width"] = "500px";
ifBrowser.Attributes["height"] = "500px";
ifBrowser.ID = "ifBrowser";
secondRowfirstCell.Controls.Add(ifBrowser);
secondRowfirstCell.ColSpan = 2;
secondRow.Controls.Add(secondRowfirstCell);
this.Controls.Add(mainTable);
base.CreateChildControls();
}
public abstract class EmbeddedUserControl : CompositeControl
{
protected Control UserControls;
public T Control<T>(string id) where T : Control
{
this.EnsureChildControls();
return (T)UserControls.FindControl(id);
}
protected override void CreateChildControls()
{
Assembly assembly = Assembly.GetExecutingAssembly();
string name = this.GetType().Namespace + "." + this.GetType().Name + ".ascx";
using (StreamReader sr = new StreamReader(assembly.GetManifestResourceStream(name)))
{
string content = sr.ReadToEnd();
UserControls = (DesignMode) ? new LiteralControl(content) : Page.ParseControl(content);
}
this.Controls.Add(UserControls);
base.CreateChildControls();
}
}
Re: Events February 15, 2012 by Christoff Truter
Hi there, wow its been quite a while since I've looked at this post hehe. I imagine to solve this issue you will most likely have to look into something like event bubbling, seen over here -> http://cstruter.com/blog/248 But I will have a look at it for you as soon as I get a gap.