<?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>A Better Way &#187; Ajax Master Pages</title>
	<atom:link href="http://www.thejacklawson.com/index.php/tag/ajax-master-pages/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thejacklawson.com</link>
	<description>Finding a better way to get things done, a technical journey</description>
	<lastBuildDate>Wed, 21 Jul 2010 14:36:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>User Controls Rock: Legos and Fake Ajax Master Pages</title>
		<link>http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/</link>
		<comments>http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 01:13:35 +0000</pubDate>
		<dc:creator>Jack</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[IIS and Windows Technologies]]></category>
		<category><![CDATA[ajax control toolkit]]></category>
		<category><![CDATA[Ajax Master Pages]]></category>
		<category><![CDATA[asp.net 3.5]]></category>
		<category><![CDATA[user controls]]></category>

		<guid isPermaLink="false">http://www.thejacklawson.com/?p=25</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>Let me start with a simple statement: user controls are absolutely fantastic.</p>
<p>If you haven&#8217;t delved into the realm of the .ascx, then let me briefly explain the two big benefits that have been relevant to me recently:</p>
<ul>
<li>Totally reusable code</li>
<li>Emulate master pages; only with this, you can use the Ajax control toolkit to do it</li>
</ul>
<p>On the first point, the reusable code point, this is the biggest. It&#8217;s the entire reason behind user controls. It lets you chunk out code that you use all over, and use it in several places; it&#8217;s a fundamental of object oriented development. Let&#8217;s do an example.</p>
<p>Say you have a piece of code that goes to the database and retrieves data on a member. That data is then put into a styled gridview, for the administration side of a website. But, you also have an area in a members section where a user can edit their own information (more like a formview); and, on the normal web display, casual users can browse through usernames and user roles in a paged gridview.</p>
<p>Don&#8217;t write the same code thrice! (or even twice!) Rather, make a user control to do all the work for you, and place it in each of your pages. This is how I&#8217;d do it:</p>
<p><i>For the sake of this article, I&#8217;m assuming you&#8217;re using a list of &#8220;Member&#8221; objects. Use whatever.</i></p>
<p>-Make a public class-level property in your control called &#8220;ReadOnly&#8221;, a bool.<br />-Make a public class-level method in your control called &#8220;Intialize&#8221;, which takes an IList of Member objects<br />-If the IList has one object, add a FormView to your control, and databind it. Otherwise, go the Gridview route (doing this all from the codebehind would keep your code cleaner and keep you from having an extra control rendered, although you could do this in the .ascx if you really wanted to; just set one to visible=&#8221;false&#8221; to the one you&#8217;re not using)<br />-Create all of the appropriate methods for your control for delete, update, insert, etc. and set up your formview or gridview accordingly (of course, only update for our member&#8217;s area FormView!)<br />-If ReadOnly is true, don&#8217;t add edit, insert, or delete buttons to your formview/gridview. (Easy enough; could set visible=&#8221;false&#8221; on the fields, or just not add them at all)</p>
<p>And, then, back on your three main pages, register and use your control.&nbsp; Set the ReadOnly property (if it&#8217;s a bool, you should even get IntelliSense for true/false), and then on Page_Load, call Intialize on the control, passing through the IList of Members that you pulled down.</p>
<p>The reason we&#8217;re calling the data on the page, rather than the control, is so that the control can remain &#8220;dumb&#8221; and doesn&#8217;t need to know whether it&#8217;s a member, admin, or web display control. The less logic in the control, the better; just spit out the lowest common denominator.</p>
<p>Ok. Now that we&#8217;ve hit the reusable control side, let&#8217;s hit the fake-an-ajax-master-page part.</p>
<p>As you may or may not know, even if you wrap the ContentTemplate in a MasterPage, it still reloads every page change. This is because the MasterPage gets loaded <i>after</i> the rest of the page, and is treated like a control. So, if you change pages.. it loads the page, and then says afterwards, &#8216;oh, yeah, that was Ajax. Oh well.&#8217;</p>
<p>The cool thing you can do, is make one default page, and inside of that have user controls in place of your pages, something like this:</p>
<p>-Make your default.aspx page<br />-Create user controls; however, rather than logic in Page_Load of the control, put it all in a public class-level method you call Initialize.You&#8217;ll see why soon.<br />-Put all of your controls into your default.aspx page, with visible=&#8221;false&#8221;.<br />-On the onclick of your navigation buttons, run the Initialize on the control, and set it&#8217;s <i>visible</i> property to <i>true</i>, and all the others to <i>false</i>.. I might alternately suggest using Command and a single method rather than Onclick and seperate methods for each button, so you can pass through a CommandName, which you can then use in a switch statement and know what button you hit.</p>
<p>Ok. Now that we have our fancy Ajaxy page, you may be wondering: why not just visiblity? Why this Initialize thing?</p>
<p>This is because ASP.NET will fully render the controls, visible or not. Which means that if you have 10 pages, it will load those 10 pages and display one; which is a big performance hit on the server, especially if you&#8217;re loading database data into each of those 10. Having to explicitly call Initialize avoids this. It also allows you to pass through a common parameter to each of the controls (say, a title string that displays at the top of each control) .</p>
<p>Controls can certainly do much more than this; but these are the biggies for me. Have any .ascx uses of your own?</p>
<!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em> </em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Del.icio.us"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Del.icio.us" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to digg"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to digg" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to reddit"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to reddit" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Technorati"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Technorati" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;h=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Newsvine"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/newsvine.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Newsvine" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Newsvine" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Stumble Upon"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Stumble Upon" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Bookmarks"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Bookmarks" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="https://favorites.live.com/quickadd.aspx?url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Live-MSN"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/live.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Live-MSN" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Live-MSN" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages&amp;url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to SlashDot"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/slashdot.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to SlashDot" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to SlashDot" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;t=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to FaceBook"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to FaceBook" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Twitter"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Twitter" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/reader/link?url=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/&amp;title=User+Controls+Rock%3A+Legos+and+Fake+Ajax+Master+Pages&amp;srcURL=http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Buzz"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/googlebuzz.png" title="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Buzz" alt="Add 'User Controls Rock: Legos and Fake Ajax Master Pages' to Google Buzz" /></a></div>
<!-- Social Bookmarking Reloaded END -->]]></content:encoded>
			<wfw:commentRss>http://www.thejacklawson.com/index.php/2008/09/user-controls-rock-legos-and-fake-ajax-master-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
