<?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>Blog of developer Mikkel Ovesen &#187; usercontrol</title>
	<atom:link href="http://blog.ovesens.net/tag/usercontrol/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ovesens.net</link>
	<description>My thoughts, stuff I need to remember or things I just want to share with the world</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dynamically Loading ASP.NET User Controls with jQuery</title>
		<link>http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/</link>
		<comments>http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 21:17:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[usercontrol]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">/post/2008/12/26/Dynamically-Loading-ASPNET-User-Controls-with-jQuery.aspx</guid>
		<description><![CDATA[UPDATE (16-07-2009) Here is a new sample website with source code, it now disposes the page object and allows for WebControl rendering. Dynamically Loading ASP.NET User Controls with jQuery v2.zip (25.52 kb) &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Â  This post is only relevant if you &#8230; <a href="http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE (16-07-2009)</strong></p>
<p>Here is a new sample website with source code, it now disposes the page object and allows for WebControl rendering.</p>
<p><a href="/wp-content/uploads/archive/Dynamically Loading ASP.NET User Controls with jQuery v2.zip">Dynamically Loading ASP.NET User Controls with jQuery v2.zip (25.52 kb)</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Â </p>
<p>This post is only relevant if you are using WebForms. If you have converted to the MVC approach, then go read about Dependency Injection or NHibernate <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If however you are using WebForms or are maintaining an application that was build with WebForms. Here is a way to partially render a usercontrol via jQuery (ajax).</p>
<h2>Introduction</h2>
<p>User controls are a great way to group markup and functionality, but in these AJAX times, the problem and nature of User Controls, in relation to ajax, is their fixed position in the Page Control tree.</p>
<p>Sam Mueller describes the issues and gives his solution to dynamically loaded user controls using jQuery.</p>
<p>Read it here:</p>
<p><a title="http://samuelmueller.com/post/2008/12/20/Dynamically-Loading-ASPNET-User-Controls-with-jQuery.aspx" href="http://samuelmueller.com/post/2008/12/20/Dynamically-Loading-ASPNET-User-Controls-with-jQuery.aspx" target="_blank">http://samuelmueller.com/post/2008/12/20/Dynamically-Loading-ASPNET-User-Controls-with-jQuery.aspx</a></p>
<p>Sam Muellers raises some very interesting points, however I have two things I would like to change (URL and security).</p>
<h2>URL</h2>
<p>I very much like that Sam Mueller has found a solution to &#8220;directly&#8221; call a user control (view) with an URL. What I do not like is the URL to be called:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">/ajax.svc/renderuc?path=/usercontrols/myusercontrol.ascx</pre>
</div>
</div>
<p>I would like to be able to call the user control directly by this kind of URL:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">/usercontrols/myusercontrol.ascx</pre>
</div>
</div>
<p>And by using a HttpHandler this is possible. I have written an example here:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> AjaxUserControlHandler : IHttpHandler</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ProcessRequest(HttpContext context)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>         {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span>             <span style="color: #008000;">// Get the path to the user control</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span>             <span style="color: #0000ff;">string</span> path = context.Request.Url.LocalPath;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span>             <span style="color: #008000;">// Intialize the pseudo page and user control</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>             Page page = <span style="color: #0000ff;">new</span> Page();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>             UserControl viewControl = page.LoadControl(path) <span style="color: #0000ff;">as</span> UserControl;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span>             <span style="color: #008000;">// Display error if the user control was not found</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  13:</span>             <span style="color: #0000ff;">if</span> (viewControl == <span style="color: #0000ff;">null</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  14:</span>             {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  15:</span>                 context.Response.StatusCode = 404;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  16:</span>                 context.Response.Output.WriteLine(<span style="color: #006080;">"The requested url was not found"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  17:</span>                 <span style="color: #0000ff;">return</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  18:</span>             }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  19:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  20:</span>             <span style="color: #008000;">// Check existense of the AjaxEnabled attribute. Only add the AjaxEnabled attribut to </span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  21:</span>             <span style="color: #008000;">// user controls that is safe for direct calls</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  22:</span>             var type = viewControl.GetType();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  23:</span>             var attributes = type.GetCustomAttributes(<span style="color: #0000ff;">typeof</span> (AjaxEnabledAttribute), <span style="color: #0000ff;">true</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  24:</span>             AjaxEnabledAttribute attribute = attributes.FirstOrDefault() <span style="color: #0000ff;">as</span> AjaxEnabledAttribute;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  25:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  26:</span>             <span style="color: #008000;">// If the attribute was not found, display an error</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  27:</span>             <span style="color: #0000ff;">if</span> (attribute == <span style="color: #0000ff;">null</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  28:</span>             {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  29:</span>                 context.Response.StatusCode = 403;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  30:</span>                 context.Response.Output.WriteLine(<span style="color: #006080;">"Access to the resource is not allowed"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  31:</span>                 <span style="color: #0000ff;">return</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  32:</span>             }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  33:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  34:</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  35:</span>             <span style="color: #008000;">// Check if the request is valiud with regards to the requirements of the attribute.</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  36:</span>             <span style="color: #008000;">// If not, display error</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  37:</span>             <span style="color: #0000ff;">if</span> ((attribute.Method == RequestMethodSupport.GET &amp;&amp; context.Request.RequestType != <span style="color: #006080;">"GET"</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  38:</span>                 || (attribute.Method == RequestMethodSupport.POST &amp;&amp; context.Request.RequestType != <span style="color: #006080;">"POST"</span>))</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  39:</span>             {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  40:</span>                 context.Response.StatusCode = 403;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  41:</span>                 context.Response.Output.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"The request method {0} is not allowed."</span>, context.Request.RequestType));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  42:</span>                 <span style="color: #0000ff;">return</span>;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  43:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  44:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  45:</span>             <span style="color: #008000;">// Add user control to the pages control tree</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  46:</span>             page.Controls.Add(viewControl);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  47:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  48:</span>             <span style="color: #008000;">// Disable caching, remove this if you will allow client caching</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  49:</span>             context.Response.CacheControl = <span style="color: #006080;">"No-Cache"</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  50:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  51:</span>             <span style="color: #008000;">// Execute and return result to Output stream</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  52:</span>             context.Server.Execute(page, context.Response.Output, <span style="color: #0000ff;">true</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  53:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  54:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  55:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> IsReusable</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  56:</span>         {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  57:</span>             get { <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  58:</span>         }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  59:</span>     }</pre>
</div>
</div>
<p>To make it work, you need to add the following to httpHandlers tag in the web.config.</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">verb</span><span class="kwrd">="*"</span> <span class="attr">path</span><span class="kwrd">="*.ascx"</span> <span class="attr">type</span><span class="kwrd">="[NAMESPACE].AjaxUserControlHandler, [ASSEMBLY]"</span><span class="kwrd">/&gt;</span></pre>
<h2>Security</h2>
<p>I have implemented an AjaxEnabled attribut, so that it is not possible to directly call any of the user controls (if you know the path to them) in the solution.</p>
<p>The attribute is simple and looks like this:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> AjaxEnabledAttribute : Attribute</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>     [DefaultValue(RequestMethodSupport.All)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>     <span style="color: #0000ff;">public</span> RequestMethodSupport Method { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span> }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">enum</span> RequestMethodSupport</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>     All,</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>     GET,</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span>     POST</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span> }</pre>
</div>
</div>
<p>By doing this, the handler will only allow calls to user controls that have the [AjaxEnabled[ attribute, like this:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> [AjaxEnabled]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> Test : System.Web.UI.UserControl</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span> {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>     ...</pre>
</div>
</div>
<h2>Conclusion</h2>
<p>With the HttpHandler, and the attribute in place, my two concerns (url and security) are dealt with.</p>
<p>You are now able to call a user control directly:</p>
<p>/usercontrols/myusercontrol.ascx</p>
<p>And you are only able to call user controls that have the AjaxEnabled attribute.</p>
<p>Any comments and suggestions are welcome btw <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Dynamic controls in ASP.NET</title>
		<link>http://blog.ovesens.net/2008/11/dynamic-controls-in-asp-net/</link>
		<comments>http://blog.ovesens.net/2008/11/dynamic-controls-in-asp-net/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 19:42:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[usercontrol]]></category>

		<guid isPermaLink="false">/post/2008/11/25/Dynamic-controls-in-ASPNET.aspx</guid>
		<description><![CDATA[As many articles point out it is important to load dynamic control in the Init event of the life cycle. This is important if you want to preserve the view state. Another thing I found out&#8230; you need to instantiate &#8230; <a href="http://blog.ovesens.net/2008/11/dynamic-controls-in-asp-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
As many articles point out it is important to load dynamic control in the Init event of the life cycle. This is important if you want to preserve the view state.
</p>
<p>
Another thing I found out&#8230; you need to instantiate the control, add it to a ControlCollection and then set the properties of the control. If you set the properties before it is added to a ControlCollection. The view state does not work.
</p>
<h2>This is wrong</h2>
<p>
BaseProductControl<font size="2"> control = (<font color="#2b91af"><font color="#2b91af">BaseProductControl</font></font>)<font color="#0000ff"><font color="#0000ff">this</font></font>.Page.LoadControl(product.ViewControl);<br />
control.ProductId = productId;<br />
</font><font size="2">ProductViewPlaceHolder.Controls.Add(control);</font>
</p>
<h2>This will work</h2>
<p><font size="2" color="#2b91af"><font size="2" color="#2b91af"></p>
<p>
BaseProductControl<font size="2"> control = (</font><font size="2" color="#2b91af"><font size="2" color="#2b91af">BaseProductControl</font></font><font size="2">)</font><font size="2" color="#0000ff"><font size="2" color="#0000ff">this</font></font><font size="2">.Page.LoadControl(product.ViewControl);<br />
ProductViewPlaceHolder.Controls.Add(control);<br />
control.ProductId = productId;</font><font size="2"></font>
</p>
<p></font></font></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/11/dynamic-controls-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Do I: Build Custom Server Controls that Work With or Without ASP.NET AJAX?</title>
		<link>http://blog.ovesens.net/2007/09/how-do-i-build-custom-server-controls-that-work-with-or-without-asp-net-ajax/</link>
		<comments>http://blog.ovesens.net/2007/09/how-do-i-build-custom-server-controls-that-work-with-or-without-asp-net-ajax/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 14:37:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[usercontrol]]></category>

		<guid isPermaLink="false">/post/2007/09/19/How-Do-I-Build-Custom-Server-Controls-that-Work-With-or-Without-ASPNET-AJAX.aspx</guid>
		<description><![CDATA[View original post here: http://www.asp.net/learn/ajax-videos/video-186.aspx &#8212; Do you have an idea for a custom server control that should be able to work without ASP.NET AJAX, yet could take advantage of ASP.NET AJAX if it is available? In&#160;this video we learn &#8230; <a href="http://blog.ovesens.net/2007/09/how-do-i-build-custom-server-controls-that-work-with-or-without-asp-net-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="left_video_column_wide">
View original post here:<br />
<a href="http://www.asp.net/learn/ajax-videos/video-186.aspx">http://www.asp.net/learn/ajax-videos/video-186.aspx</a>
</div>
<div class="left_video_column_wide">
&#8212;
</div>
<div class="left_video_column_wide">
<a href="http://download.microsoft.com/download/3/0/f/30f85549-1b3b-4ce6-ab28-209b5c874c73/WinVideo-ASP-AJAXEnableYourExistingCustomControl.wmv"><img class="thumbnail_264_video" src="http://static.asp.net/asp.net/images/videos/video-186.png" alt="Play Video" width="264" height="156" /></a>
</div>
<div class="right_video_column_wide">
<p>
Do you have an idea for a custom server control that should be able to work without ASP.NET AJAX, yet could take advantage of ASP.NET AJAX if it is available? In&nbsp;this video we learn how to build a custom server control that does not reference the ASP.NET AJAX assembly, but uses Reflection to &ldquo;play nice&rdquo; with the ASP.NET AJAX UpdatePanel.
</p>
<p>
<em>Presented by Chris Pels</em>
</p>
<p>
Duration: 19 minutes, 21 seconds
</p>
<p>
Date: 9 July 2007
</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2007/09/how-do-i-build-custom-server-controls-that-work-with-or-without-asp-net-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

