CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
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 from the demo code, 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> If we look at the following crude examples. We can override the render method and make use of the methods exposed by the HtmlTextWriter parameter, like in example 1. One might even consider to override the CreateChildControls method, and adding the elements to the controls collection, like in example 2. Example 1 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); } Example 2 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(); } I've never liked any of these approaches, it just feels like a very "unnatural" way to represent HTML. It is however possible to add some real design-time support of our own. My initial thoughts were that one would be able to simply embed an user control (ascx) as a web resource inside an assembly and simply use it like that. Which would be a logical choice, since user controls have full design-time support for authoring. It wasn't a simple case of embed and off you go, I ran into a few difficulties, but came up with a very simple solution. Have a look at the following abstract class (we're only going to use this as a base class for embedded user controls). 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(); } } What happens here is we override the CreateChildControls method, and parse an embedded ascx file, which will act as our designer. You will also notice a generic method, it simply functions as wrapper, and ensures that the controls collection are fully populated, before we try to use them. Creating a new control will work like this: Step 1: Add an blank ascx file to your control library (you will notice there isn't an option for ascx in the list (if you did it right, hint control library project :P), choose text file and just name it eg browser.ascx) This will allow you to drag & drop controls onto the page, and work with all the nice little visual tools in VS. Step 2: ENSURE THAT THE BUILT ACTION OF THE ASCX PAGE IS SET AS "Embedded Resource" Step 3: Add a class and inherit from the EmbeddedUserControl class, GIVE IT THE SAME NAME AS WHAT YOU GAVE THE ASCX FILE eg browser.cs, obviously not the same extension thought ;) Step 4: Code away. For the purpose of this post, I created a composite control that acts like a little browser window. Which you can download here.
<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(); } }
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43