<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>deepdark.net - James Green's Blog - ASP.Net Controls</title>
    <link>http://deepdark.net/</link>
    <description>.NET, SQL Server and *.*</description>
    <image>
      <url>http://deepdark.net/files/deepdark.jpg</url>
      <title>deepdark.net - James Green's Blog - ASP.Net Controls</title>
      <link>http://deepdark.net/</link>
    </image>
    <language>en-us</language>
    <copyright>James Green</copyright>
    <lastBuildDate>Fri, 19 Jan 2007 03:55:26 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>James.Green@deepdark.net</managingEditor>
    <webMaster>James.Green@deepdark.net</webMaster>
    <item>
      <trackback:ping>http://deepdark.net/Trackback.aspx?guid=7a622312-767c-47c4-b1ec-b97465ab6d3d</trackback:ping>
      <pingback:server>http://deepdark.net/pingback.aspx</pingback:server>
      <pingback:target>http://deepdark.net/PermaLink,guid,7a622312-767c-47c4-b1ec-b97465ab6d3d.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://deepdark.net/CommentView,guid,7a622312-767c-47c4-b1ec-b97465ab6d3d.aspx</wfw:comment>
      <wfw:commentRss>http://deepdark.net/SyndicationService.asmx/GetEntryCommentsRss?guid=7a622312-767c-47c4-b1ec-b97465ab6d3d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is part two in <a href="http://deepdark.net/CategoryView,category,ASP.Net%2BControls.aspx">a
series of posts about ASP.Net Controls</a>.
</p>
        <p>
          <font color="#000000">
            <strong>ASP.Net Controls, Part 2: Composite Custom Controls
- some assembly required<font size="2"></font></strong>
          </font>
        </p>
        <p>
          <font color="#000000">Now these controls are able to be designed to have some reuse
and life outside one particular project.  The up side is that you can piece together
odds and ends from <font face="Courier New">System.Web.Ui.WebControls</font> just
how you like them, and as per normal the actual rendering to HTML is left to ASP.Net.</font>
        </p>
        <p>
          <font color="#000000">The down side is you just start with a class, descend from WebControl
and piece it together in code.</font>
        </p>
        <p>
Here's a bare bones sample to illustrate the concept, but first some <font color="#000000">notes:</font></p>
        <ul>
          <li>
            <font color="#000000">Import <font face="Courier New">System.ComponentModel</font> for
the attributes on the properties.  This is how they display in your Visual Studio
properties window</font>
          </li>
          <li>
            <font color="#000000">Use actual controls as backing variables for the properties. 
You want to be holding instances of the controls you will display in your class, and
then abstract their properties behind your own properties.</font>
          </li>
          <li>
            <font color="#000000">
              <font face="Courier New">CreateChildControls</font> is where
you assemble what will eventually be rendered down to the browser by adding to the <font face="Courier New">Controls</font> collection
exposed by the base class.</font>
          </li>
          <li>
            <font color="#000000">Call <font color="#003300"><font face="Courier New">EnsureChildControls</font>.</font> 
A lot.  :-)  </font>
          </li>
          <li>
            <font color="#000000">This sample control no behaviour.  Use <font face="Courier New">+=</font> / <font face="Courier New">AddHandler</font> to
wire up your controls to event handlers.  Omitted for clarity.</font>
          </li>
        </ul>
        <pre>
          <font color="#0000ff">using</font> System.Web.UI; <font color="#0000ff">using</font> System.Web.UI.WebControls; <font color="#0000ff">using</font> System.ComponentModel; <font color="#0000ff">namespace</font> MyProject.Controls
{ <font color="#0000ff">public</font><font color="#0000ff">class</font><font color="#000080">CompositeControl</font> :
System.Web.UI.WebControls.<font color="#000080">WebControl</font> { <font color="#000080">TextBox</font> txtInput
= <font color="#0000ff">new </font><font color="#000080">TextBox</font>(); <font color="#000080">Button</font> cmdSubmit
= <font color="#0000ff">new </font><font color="#000080">Button</font>(); <font color="#0000ff">protected
override void</font> CreateChildControls() { EnsureChildControls(); txtInput.Text
= DefaultText; Controls.Add(txtInput); cmdSubmit.Text = "Submit"; Controls.Add(cmdSubmit); <font color="#0000ff">base</font>.CreateChildControls();
} <font color="#0000ff">protected override void</font> Render(<font color="#000080">HtmlTextWriter</font> writer)
{ <font color="#0000ff">base</font>.Render(writer); } [<font color="#000080">Category</font>(<font color="#ffa500">"Appearance"</font>), <font color="#000080">DefaultValue</font>(<font color="#ffa500">"Enter
text here"</font>)] <font color="#0000ff">public string</font> DefaultText { <font color="#0000ff">get</font> {
EnsureChildControls(); <font color="#0000ff">return</font> txtInput.Text; } <font color="#0000ff">set</font> {
EnsureChildControls(); txtInput.Text = <font color="#0000ff">value</font>; } } } } </pre>
      </body>
      <title>ASP.Net Controls, Part 2: Composite Custom Controls - some assembly required</title>
      <guid isPermaLink="false">http://deepdark.net/PermaLink,guid,7a622312-767c-47c4-b1ec-b97465ab6d3d.aspx</guid>
      <link>http://deepdark.net/PermaLink,guid,7a622312-767c-47c4-b1ec-b97465ab6d3d.aspx</link>
      <pubDate>Fri, 19 Jan 2007 03:55:26 GMT</pubDate>
      <description>&lt;p&gt;
This is part two in &lt;a href="http://deepdark.net/CategoryView,category,ASP.Net%2BControls.aspx"&gt;a
series of posts about ASP.Net Controls&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;strong&gt;ASP.Net Controls, Part 2: Composite Custom Controls -
some assembly required&lt;font size=2&gt; &lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Now these controls are able to be designed to have some reuse
and life outside one particular project.&amp;nbsp; The up side is that you can piece together
odds and ends from &lt;font face="Courier New"&gt;System.Web.Ui.WebControls&lt;/font&gt; just
how you like them, and as per normal the actual rendering to HTML is left to ASP.Net.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;The down side is you just start with a class, descend from WebControl
and piece it together in code.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Here's a bare bones sample to illustrate the concept, but first some&amp;nbsp;&lt;font color=#000000&gt;notes:&lt;/font&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;font color=#000000&gt;Import &lt;font face="Courier New"&gt;System.ComponentModel&lt;/font&gt; for
the attributes on the properties.&amp;nbsp; This is how they display in your Visual Studio
properties window&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font color=#000000&gt;Use actual controls as backing variables for the properties.&amp;nbsp;
You want to be holding instances of the controls you will display in your class, and
then abstract their properties behind your own properties.&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font color=#000000&gt;&lt;font face="Courier New"&gt;CreateChildControls&lt;/font&gt; is where you
assemble what will eventually be rendered down to the browser by adding to the &lt;font face="Courier New"&gt;Controls&lt;/font&gt; collection
exposed by the base class.&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font color=#000000&gt;Call&amp;nbsp;&lt;font color=#003300&gt;&lt;font face="Courier New"&gt;EnsureChildControls&lt;/font&gt;.&lt;/font&gt;&amp;nbsp;
A lot.&amp;nbsp; :-)&amp;nbsp; &lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font color=#000000&gt;This sample control no behaviour.&amp;nbsp; Use &lt;font face="Courier New"&gt;+=&lt;/font&gt; / &lt;font face="Courier New"&gt;AddHandler&lt;/font&gt; to
wire up your controls to event handlers.&amp;nbsp; Omitted for clarity.&lt;/font&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;font color=#0000ff&gt;using&lt;/font&gt; System.Web.UI; &lt;font color=#0000ff&gt;using&lt;/font&gt; System.Web.UI.WebControls; &lt;font color=#0000ff&gt;using&lt;/font&gt; System.ComponentModel; &lt;font color=#0000ff&gt;namespace&lt;/font&gt; MyProject.Controls
{ &lt;font color=#0000ff&gt;public&lt;/font&gt; &lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#000080&gt;CompositeControl&lt;/font&gt; :
System.Web.UI.WebControls.&lt;font color=#000080&gt;WebControl&lt;/font&gt; { &lt;font color=#000080&gt;TextBox&lt;/font&gt; txtInput
= &lt;font color=#0000ff&gt;new &lt;/font&gt;&lt;font color=#000080&gt;TextBox&lt;/font&gt;(); &lt;font color=#000080&gt;Button&lt;/font&gt; cmdSubmit
= &lt;font color=#0000ff&gt;new &lt;/font&gt;&lt;font color=#000080&gt;Button&lt;/font&gt;(); &lt;font color=#0000ff&gt;protected
override void&lt;/font&gt; CreateChildControls() { EnsureChildControls(); txtInput.Text
= DefaultText; Controls.Add(txtInput); cmdSubmit.Text = "Submit"; Controls.Add(cmdSubmit); &lt;font color=#0000ff&gt;base&lt;/font&gt;.CreateChildControls();
} &lt;font color=#0000ff&gt;protected override void&lt;/font&gt; Render(&lt;font color=#000080&gt;HtmlTextWriter&lt;/font&gt; writer)
{ &lt;font color=#0000ff&gt;base&lt;/font&gt;.Render(writer); } [&lt;font color=#000080&gt;Category&lt;/font&gt;(&lt;font color=#ffa500&gt;"Appearance"&lt;/font&gt;), &lt;font color=#000080&gt;DefaultValue&lt;/font&gt;(&lt;font color=#ffa500&gt;"Enter
text here"&lt;/font&gt;)] &lt;font color=#0000ff&gt;public string&lt;/font&gt; DefaultText { &lt;font color=#0000ff&gt;get&lt;/font&gt; {
EnsureChildControls(); &lt;font color=#0000ff&gt;return&lt;/font&gt; txtInput.Text; } &lt;font color=#0000ff&gt;set&lt;/font&gt; {
EnsureChildControls(); txtInput.Text = &lt;font color=#0000ff&gt;value&lt;/font&gt;; } } } } &lt;/pre&gt;</description>
      <comments>http://deepdark.net/CommentView,guid,7a622312-767c-47c4-b1ec-b97465ab6d3d.aspx</comments>
      <category>ASP.Net Controls</category>
      <category>Geeking Out!</category>
    </item>
    <item>
      <trackback:ping>http://deepdark.net/Trackback.aspx?guid=bb33a02c-a603-4d6f-8a9e-d73199f871df</trackback:ping>
      <pingback:server>http://deepdark.net/pingback.aspx</pingback:server>
      <pingback:target>http://deepdark.net/PermaLink,guid,bb33a02c-a603-4d6f-8a9e-d73199f871df.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://deepdark.net/CommentView,guid,bb33a02c-a603-4d6f-8a9e-d73199f871df.aspx</wfw:comment>
      <wfw:commentRss>http://deepdark.net/SyndicationService.asmx/GetEntryCommentsRss?guid=bb33a02c-a603-4d6f-8a9e-d73199f871df</wfw:commentRss>
      <title>ASP.Net Controls, Part 1:  Web User Controls (ASCX files), the low hanging fruit </title>
      <guid isPermaLink="false">http://deepdark.net/PermaLink,guid,bb33a02c-a603-4d6f-8a9e-d73199f871df.aspx</guid>
      <link>http://deepdark.net/PermaLink,guid,bb33a02c-a603-4d6f-8a9e-d73199f871df.aspx</link>
      <pubDate>Fri, 19 Jan 2007 03:00:54 GMT</pubDate>
      <description>&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;It
occurred to me that ASP.Net has been a topic missing from my blog, so to rectify the
situation I'm posting three pieces on Controls in the ASP.Net space.&amp;nbsp; Took me
ages to get this straight in my own head so with any luck it will make sense here.&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;Part
1:&amp;nbsp; Web User Controls (ASCX files), the low hanging fruit &lt;/span&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;What
better place to start.&amp;nbsp; These are the odd one out in the ASP.Net control space
for a couple of reasons.&amp;nbsp; &lt;/span&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;While
it is possible, they don't do reuse between projects well.&amp;nbsp; &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;They
do have a drag and drop design time experience!&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;You
drag them onto a page from the Solution Explorer, not the toolbox.&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Verdana','sans-serif'"&gt;First
thing to note is they have their own life cycle after &lt;font face="Courier New"&gt;Page_Load&lt;/font&gt;,
and it&amp;nbsp;feels a little like &lt;font face="Courier New"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;Server.Execute()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
While they do have their own code behind class that inherits from &lt;font face="Courier New"&gt;System.Web.UI.UserControl&lt;/font&gt;,
I try to not use too many properties on the class because I feel it gives a misleading
impression in implementation.&amp;nbsp; Just say the thing has a &lt;font face="Courier New"&gt;text() &lt;/font&gt;property
and in my &lt;font face="Courier New"&gt;Page_Load&lt;/font&gt; I set the text to something, well
after &lt;font face="Courier New"&gt;Page_Load &lt;/font&gt;the thing can do what ever it wants
to and then I have to go looking for what happened to my value for the &lt;font face="Courier New"&gt;text() &lt;/font&gt;property.&gt;&gt;
&lt;/p&gt;
&lt;p&gt;
Re-use between projects requires some planning because they are not seperate from
your web project, but re-use inside the same project is what they are best at.&amp;nbsp;
Things like a navigational device, a customer lookup widget or something that makes
an appearance&amp;nbsp;more than once are good examples.&amp;nbsp; Pull them out, glue them
together with a Web User Control, drop them back in.&amp;nbsp; Easy.&amp;nbsp; 
&lt;/p&gt;</description>
      <comments>http://deepdark.net/CommentView,guid,bb33a02c-a603-4d6f-8a9e-d73199f871df.aspx</comments>
      <category>Geeking Out!</category>
      <category>ASP.Net Controls</category>
    </item>
  </channel>
</rss>