<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Married Geek » ASP.NET, jQuery, Web, JavaScript, and CSS blog &#187; server control</title>
	<atom:link href="http://marriedgeek.com/index.php/tag/server-control/feed/" rel="self" type="application/rss+xml" />
	<link>http://marriedgeek.com</link>
	<description></description>
	<lastBuildDate>Thu, 14 Apr 2011 15:58:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Extending A Native Web Control</title>
		<link>http://marriedgeek.com/2009/07/extending-a-native-web-control/</link>
		<comments>http://marriedgeek.com/2009/07/extending-a-native-web-control/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 15:17:54 +0000</pubDate>
		<dc:creator>_theMarriedGeek</dc:creator>
				<category><![CDATA[ASP.NET and Web]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[custom control]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[server control]]></category>
		<category><![CDATA[user control]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.marriedgeek.com/?p=122</guid>
		<description><![CDATA[Some may not know the difference but user controls are different from server controls. User controls inherit from the UserControl class while server controls can inherit from, say, the Button, DropDownList, GridView, or, well, any Control or WebControl object. Using a server control is faster than using a user control since there is no designer [...]]]></description>
			<content:encoded><![CDATA[<p>Some may not know the difference but user controls are different from server controls. User controls inherit from the UserControl class while server controls can inherit from, say, the Button, DropDownList, GridView, or, well, any Control or WebControl object. Using a server control is faster than using a user control since there is no designer UI to be compiled.</p>
<p>The main drawback from creating a server control versus a user control is the lack of building a rich UI in most respects. Because there isn&#8217;t a .ascx page to load existing controls onto, you don&#8217;t have this ability as easily as you do with a server control (unless you programmatically add controls to a placeholder object in a composite control).</p>
<p>The main difference is that your control must inherit from an existing Control or WebControl object, or one that inherits from these, such as a Button.</p>
<p>Here&#8217;s a quick example of a DropDownList that is pre-populated with data essential to determining sex &#8211; &#8220;Male&#8221; and &#8220;Female&#8221;. You can save this as a .CS file into your app_code directory. In this example, we are essentially extending the existing DropDownList control to add additional functionality.</p>
<pre>using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;

namespace CustomControls
{
    public class GenderList : DropDownList
    {
        public GenderList()
        {
            System.Collections.Generic.Dictionary&lt;String, String&gt; dctGender = new System.Collections.Generic.Dictionary&lt;String, String&gt;();

            dctGender.Add("MALE", "Male");
            dctGender.Add("FEMALE", "Female");
            dctGender.Add("NA", "No response");

            base.DataSource = dctGender;
            base.DataTextField = "value";
            base.DataValueField = "key";
            base.DataBind();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}</pre>
<p>Now, to use it on your page, you&#8217;ll need to either Register the control using the &lt;%@ Register %&gt; declaration or add a reference in the &lt;pages&gt;&lt;controls&gt; &#8230; &lt;/controls&gt;&lt;/pages&gt; section of your web.config.</p>
<pre>&lt;%@ Register TagPrefix="asp" Namespace="CustomControls" %&gt;</pre>
<p>&#8230;or in your web.config&#8230;</p>
<pre>&lt;pages&gt;
    &lt;controls&gt;
        ....
        &lt;add tagPrefix="asp" namespace="CustomControls"/&gt;
    &lt;/controls&gt;
&lt;/pages&gt;</pre>
<p>Notice we did not include the assembly. This is because it was not compiled into a .dll. <strong>If it is compiled into a .dll, you must include the Assembly name. </strong></p>
<p><a href="http://www.marriedgeek.com/wp-content/uploads/2009/07/genderlist.gif"><img class="size-full wp-image-130 alignright" title="genderlist" src="http://www.marriedgeek.com/wp-content/uploads/2009/07/genderlist.gif" alt="genderlist" width="119" height="87" /></a></p>
<p>After that, if you don&#8217;t feel like waiting for your intellisense to update, you can simply insert the control onto your page. The only difference is, you do not need to include the <strong>src</strong> attribute, since the control will be compiled (just the TagPrefix, and Namespace).</p>
<p>The class above is very simple. It inherits from DropDownList, so it has all the functionality of the native DropDownList class, but binds some data in the constructor. Pretty simple, but will save a lot of time when working with forms. If this seem a bit boring, another idea could be to have a StateList control that is populated with states from an XML file. Or, perhaps you find yourself always setting default attributes in your GridViews, such as GridLines=&#8221;none&#8221; or AutoGenerateColumns=&#8221;false&#8221;. Make your own and have these defaults set for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://marriedgeek.com/2009/07/extending-a-native-web-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
